Documentation
¶
Overview ¶
Package openrouter provides an OpenRouter API client implementing the ai.Provider interface.
OpenRouter (https://openrouter.ai) is a unified gateway that fronts ~100 LLMs from many vendors behind an OpenAI-compatible Chat Completions API. This client is a thin Chat Completions adapter — it does not attempt to support OpenAI's Responses API or any vendor-specific extensions other than the OpenRouter-specific `cached_tokens` and `cost` fields surfaced in the response.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client implements ai.Provider for OpenRouter's unified Chat Completions API.
Unlike the OpenAI client there is no API-type detection — OpenRouter is Chat-Completions only. Image generation is not supported; callers should use a Gemini image model instead.
func NewClient ¶
func NewClient(apiKey string, opts ...ClientOption) *Client
NewClient creates a new OpenRouter client.
func (*Client) NewHandler ¶
NewHandler creates an ai.Handler wrapping this client.
func (*Client) Step ¶ added in v0.15.2
Step is the multi-turn / tool-aware completion entry point introduced by M-AI-TOOL-LOOP (v0.17.0). OpenRouter speaks OpenAI Chat Completions natively, so we reuse the shared translation helpers from internal/ai/openai (BuildChatStepRequest, ParseChatStepResponse, MapChatFinishReason, ClassifyChatHTTPErrorFor) and compose them with OpenRouter's two extensions:
- top-level `provider` field (translated from req.Routing via translatePolicy)
- HTTP-Referer and X-Title attribution headers (lifted from env vars, same as the existing Generate path)
Errors are returned as *ai.AIError exclusively. Non-2xx responses are classified via ai.ClassifyHTTPError; transport / context errors via ai.ClassifyError. The inner error.message field of an OpenAI-format error envelope, when present, is hoisted into the AIError.Message for clarity.
Routing composition: when req.Routing is non-zero, translatePolicy emits a providerField that rides alongside Tools / Messages on the wire. A routed model like "anthropic/claude-sonnet-4.5" with a tool catalog works end-to-end against the OpenRouter URL.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption configures a Client.
func WithBaseURL ¶
func WithBaseURL(url string) ClientOption
WithBaseURL sets a custom base URL (useful for testing).
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) ClientOption
WithHTTPClient sets a custom HTTP client.