openrouter

package
v0.24.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 13 Imported by: 0

README

OpenRouter Provider

OpenRouter provider for the LLM abstraction library.

Configuration

import "github.com/codewandler/llm/provider/openrouter"

// Create provider with default model (anthropic/claude-sonnet-4.5)
p := openrouter.New("your-api-key")

// Or specify a custom default model
p := openrouter.New("your-api-key").WithDefaultModel("openai/gpt-4o")

// Get the default model
defaultModel := p.DefaultModel()

Constants

  • openrouter.DefaultModel = "anthropic/claude-sonnet-4.5"

Models

The models.json file contains a curated list of 229 models that support tool calling on OpenRouter.

Generation

The file was generated using the OpenRouter models API with full model data:

curl -s "https://openrouter.ai/api/v1/models" | \
  jq '[.data[] | select(.supported_parameters != null and (.supported_parameters | index("tools")))]' \
  > models.json
Model Data Structure

Each model includes complete information:

  • ID and name
  • Context length and max completion tokens
  • Pricing (prompt, completion, cache read)
  • Supported parameters (tools, temperature, etc.)
  • Architecture details (modality, tokenizer)
  • Default parameters
Accessing Model Data
// Get basic model list (ID, Name, Provider)
models := p.Models()  // Returns 229 tool-enabled models

// Get detailed model data with pricing, context, etc.
modelData, err := openrouter.GetModelData()
for _, m := range modelData {
    fmt.Printf("%s: %d tokens, $%s per prompt token\n", 
        m.Name, m.ContextLength, m.Pricing.Prompt)
}
Contents

The file includes popular models with tool support from:

  • Anthropic (Claude Opus, Sonnet, Haiku variants)
  • OpenAI (GPT-4, GPT-4o, o3-mini, etc.)
  • Google (Gemini models)
  • Meta (Llama models)
  • Mistral (Mistral, Mixtral models)
  • Qwen (Qwen3 models)
  • DeepSeek (DeepSeek models)
  • And many more providers (229 total)
Usage

The embedded Models() function returns all 229 tool-enabled models from models.json. For real-time data from the API, use FetchModels().

Documentation

Index

Constants

View Source
const (

	// DefaultModel is the recommended default model for OpenRouter
	DefaultModel = "anthropic/claude-sonnet-4.5"
)

Variables

This section is empty.

Functions

func DefaultOptions added in v0.12.0

func DefaultOptions() []llm.Option

DefaultOptions returns the default options for OpenRouter. Base URL defaults to https://openrouter.ai/api. API key should be provided via WithAPIKey() or WithAPIKeyFunc().

Types

type ModelData

type ModelData struct {
	ID            string `json:"id"`
	CanonicalSlug string `json:"canonical_slug"`
	HuggingFaceID string `json:"hugging_face_id"`
	Name          string `json:"name"`
	Created       int64  `json:"created"`
	Description   string `json:"description"`
	ContextLength int    `json:"context_length"`
	Architecture  struct {
		Modality         string   `json:"modality"`
		InputModalities  []string `json:"input_modalities"`
		OutputModalities []string `json:"output_modalities"`
		Tokenizer        string   `json:"tokenizer"`
		InstructType     *string  `json:"instruct_type"`
	} `json:"architecture"`
	Pricing struct {
		Prompt         string `json:"prompt"`
		Completion     string `json:"completion"`
		InputCacheRead string `json:"input_cache_read"`
	} `json:"pricing"`
	TopProvider struct {
		ContextLength       int  `json:"context_length"`
		MaxCompletionTokens int  `json:"max_completion_tokens"`
		IsModerated         bool `json:"is_moderated"`
	} `json:"top_provider"`
	PerRequestLimits    interface{}            `json:"per_request_limits"`
	SupportedParameters []string               `json:"supported_parameters"`
	DefaultParameters   map[string]interface{} `json:"default_parameters"`
}

ModelData represents the full structure of a model from OpenRouter API

func GetModelData

func GetModelData() ([]ModelData, error)

GetModelData returns the full model data from the embedded models.json file. This includes pricing, context length, supported parameters, and other metadata.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements the OpenRouter LLM backend.

func New

func New(opts ...llm.Option) *Provider

New creates a new OpenRouter provider. Options are applied on top of DefaultOptions().

Example usage:

// With API key
p := openrouter.New(llm.WithAPIKey("sk-or-..."))

// With API key from environment
p := openrouter.New(llm.APIKeyFromEnv("OPENROUTER_API_KEY"))

// With dynamic API key resolution
p := openrouter.New(llm.WithAPIKeyFunc(func(ctx context.Context) (string, error) {
    return secretStore.Get(ctx, "openrouter-key")
}))

func (*Provider) CountTokens added in v0.24.0

func (p *Provider) CountTokens(_ context.Context, req llm.TokenCountRequest) (*llm.TokenCount, error)

CountTokens estimates the number of input tokens for the given request.

The BPE encoding is selected based on the model ID (o200k_base for GPT-4o / o-series; cl100k_base for everything else including unknown models). No per-message overhead is applied — OpenRouter routes to many providers and the overhead constants are model-family-specific.

func (*Provider) CreateStream

func (p *Provider) CreateStream(ctx context.Context, opts llm.StreamRequest) (<-chan llm.StreamEvent, error)

func (*Provider) DefaultModel

func (p *Provider) DefaultModel() string

DefaultModel returns the configured default model ID.

func (*Provider) FetchModels

func (p *Provider) FetchModels(ctx context.Context) ([]llm.Model, error)

func (*Provider) Models

func (p *Provider) Models() []llm.Model

Models returns the curated list of tool-enabled models from the embedded models.json file. This includes 229 models from various providers that support tool calling.

func (*Provider) Name

func (p *Provider) Name() string

func (*Provider) WithDefaultModel

func (p *Provider) WithDefaultModel(modelID string) *Provider

WithDefaultModel sets the default model to use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL