openrouter

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 11 Imported by: 0

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.

Index

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 New

func New(cfg Config) (*Adapter, error)

New builds an Adapter from config.

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

func (a *Adapter) ProviderName() string

ProviderName implements agentcore.ProviderNamer.

func (*Adapter) SupportsTools

func (a *Adapter) SupportsTools() bool

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).

Jump to

Keyboard shortcuts

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