openrouter

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package openrouter implements llm.Client against OpenRouter (https://openrouter.ai). Streaming-first; tool-call assembly; 429/5xx retry at stream-start. The SSE parser is inlined here per ADR-022.

Index

Constants

View Source
const DefaultBaseURL = "https://openrouter.ai/api/v1"

DefaultBaseURL is OpenRouter's API root.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an OpenRouter implementation of llm.Client.

func NewClient

func NewClient(opts *Options) (*Client, error)

NewClient builds a Client. Returns an error if APIKey is empty.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, req llm.Request) (<-chan llm.Event, error)

Stream sends one chat-completions request and returns a channel of events. EventEnd is always the last event; the channel closes after EventEnd.

Retry happens only at stream-start: if the HTTP response is 429 or 5xx before any SSE bytes have been read, aikido closes the body and retries per the policy returned by retryPolicy(). Mid-stream errors do NOT retry — they propagate as EventError.

type Options

type Options struct {
	// APIKey is required.
	APIKey string

	// BaseURL overrides DefaultBaseURL. Useful for tests via httptest.Server.
	BaseURL string

	// HTTPClient overrides the default. Default: &http.Client{Timeout: 0}.
	// Streams may be long-lived; callers wanting a wall-clock cap pass a
	// context with deadline rather than configuring a client-level timeout.
	HTTPClient *http.Client

	// HTTPReferer is the optional OpenRouter ranking-attribution header.
	HTTPReferer string

	// XTitle is the optional OpenRouter ranking-attribution header.
	XTitle string

	// ProviderOrder is the optional `provider.order` routing preference.
	// A single entry locks to one provider (allow_fallbacks=false); multiple
	// entries form a fallback chain (allow_fallbacks=true). Matches the
	// production pattern in asolabs/hub.
	ProviderOrder []string
}

Options configure the OpenRouter Client.

Jump to

Keyboard shortcuts

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