Skip to main content

Crate chat_openrouter

Crate chat_openrouter 

Source
Expand description

OpenRouter provider for chat-rs.

OpenRouter is a unified gateway in front of hundreds of models from many vendors; the model slug selects which one (e.g. anthropic/claude-sonnet-4, openai/gpt-4o, google/gemini-2.5-pro).

It exposes two OpenAI-compatible wires, and the builder picks one upstream and hands off to the matching wire crate — no wrapper type:

Both wire clients already implement CompletionProvider (and StreamProvider under the stream feature), so streaming is free on either path. OpenRouter has no WebSocket endpoint — streaming is SSE over HTTP — but the builder stays generic over Transport.

use chat_openrouter::OpenRouterBuilder;

// OPENROUTER_API_KEY env var is read automatically.
// Default: Responses API.
let responses = OpenRouterBuilder::new()
    .with_model("anthropic/claude-sonnet-4")
    .build();

// Opt in to the Chat Completions API.
let completions = OpenRouterBuilder::new()
    .with_completions()
    .with_model("openai/gpt-4o")
    .build();

Structs§

Completions
Wire type-state: target the Chat Completions API.
CompletionsClient
OpenRouterBuilder
ReqwestTransport
HTTP transport backed by reqwest.
Responses
Wire type-state: target the Responses API (default).
ResponsesClient
Generic client over the OpenAI Responses API wire format (POST {base_url}/responses). Provider wrappers like [chat_openai] hold one of these as their inner state.
WithModel
WithoutModel

Constants§

DEFAULT_OPENROUTER_BASE_URL
Default OpenRouter base URL. The Responses API lives at {base_url}/responses, Chat Completions at {base_url}/chat/completions.