Documentation
¶
Overview ¶
Package openrouter is the provider-native OpenRouter model adapter: an agentcore.ChatModel (llm.LLM) backed by OpenRouter's chat-completions API. OpenRouter's own API is OpenAI-shaped, so a plain net/http client is native here and needs no SDK.
It is the anti-corruption layer between OpenRouter's wire format and agentcore types. Full scope: SSE token streaming and tool calling, so any OpenRouter model id works as a primary conversational model. OpenRouter extensions beyond standard OpenAI are wired:
- the reasoning request object (effort mapped from the call's thinking level), with the model's reasoning surfaced as a thinking block and its reasoning_details echoed back on assistant messages so a model keeps its chain of thought across tool calls,
- usage accounting: cost and cached prompt tokens reach the Meter.
The wire dialect itself lives in internal/chatwire, shared with the aperture adapter: OpenRouter is the reference speaker of that dialect, and gateways that speak it differ only in endpoint, auth and attribution headers, which are this package's.
Index ¶
- type Adapter
- func (a *Adapter) Generate(ctx context.Context, msgs []ac.Message, tools []ac.ToolSpec, ...) (*ac.LLMResponse, error)
- func (a *Adapter) GenerateStream(ctx context.Context, msgs []ac.Message, tools []ac.ToolSpec, ...) (<-chan ac.StreamEvent, error)
- func (a *Adapter) ProviderName() string
- func (a *Adapter) SupportsTools() bool
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter struct {
// contains filtered or unexported fields
}
Adapter wraps OpenRouter's chat-completions API as an llm.LLM.
func (*Adapter) Generate ¶
func (a *Adapter) Generate(ctx context.Context, msgs []ac.Message, tools []ac.ToolSpec, opts ...ac.CallOption) (*ac.LLMResponse, error)
Generate runs a one-shot chat completion.
func (*Adapter) GenerateStream ¶
func (a *Adapter) GenerateStream(ctx context.Context, msgs []ac.Message, tools []ac.ToolSpec, opts ...ac.CallOption) (<-chan ac.StreamEvent, error)
GenerateStream runs a streaming chat completion, emitting the agentcore StreamEvent sequence the loop expects: thinking/text start-delta-end, tool-call start/delta/end with the completed call, a terminal done event carrying the fully assembled message with its stop reason and an error event on failure.
func (*Adapter) ProviderName ¶
ProviderName implements agentcore.ProviderNamer.
func (*Adapter) SupportsTools ¶
SupportsTools reports true: this adapter wires OpenAI-format tool calling.
type Config ¶
type Config struct {
APIKey string
Model string
BaseURL string
Referer string
Title string
// Meter, if set, receives token, cost and latency Usage for every call.
Meter llm.Meter
// Temperature and TopP are optional sampler params. nil leaves them off the
// wire (the model's own default).
Temperature *float64
TopP *float64
// StreamIdleTimeout aborts a stream that has produced nothing for this
// long, surfacing agentcore.ErrProviderStreamIdle so the agent loop can
// fail over instead of waiting on a dead connection. Zero uses two
// minutes; negative disables the watchdog. Streaming has no overall
// deadline: a legitimately long generation must not be cut off.
StreamIdleTimeout time.Duration
}
Config configures the adapter. APIKey and Model are required; Model is an OpenRouter model id such as "moonshotai/kimi-k3". BaseURL defaults to OpenRouter's public endpoint. Referer and Title are OpenRouter's optional app attribution headers (HTTP-Referer, X-Title).