v0.10.5

Architecture

Last updated


go-llm-router has three layers: the contract layer (core), the assembly layer (core/router), and the provider adapters. This page shows only the layer relationships; per-module diagrams, sequence diagrams, and state machines live in the full architecture document.

Overview


graph TB
    App[Caller] --> Router[router.New]
    Router --> Registry[newFn prefix table]
    Registry --> KeyAgents[Key-based agents]
    Registry --> OAuthAgents[OAuth agents]
    Registry --> CompatAgent[compat agent]
    Registry --> CLIAgent[claude-code agent]
    KeyAgents --> Core[core contracts]
    OAuthAgents --> Core
    CompatAgent --> Core
    CLIAgent --> Core
    CLIAgent --> CLI[claude CLI process]
    OAuthAgents --> OAuth[core/oauth]
    OAuth --> Keychain[go-pkg keychain]
    Core --> Stream[Stream event normalization]
    Core --> Usage[Usage normalization]
    Core --> Media[Image / audio interfaces]
  1. graph TB
  2. App[Caller] --> Router[router.New]
  3. Router --> Registry[newFn prefix table]
  4. Registry --> KeyAgents[Key-based agents]
  5. Registry --> OAuthAgents[OAuth agents]
  6. Registry --> CompatAgent[compat agent]
  7. Registry --> CLIAgent[claude-code agent]
  8. KeyAgents --> Core[core contracts]
  9. OAuthAgents --> Core
  10. CompatAgent --> Core
  11. CLIAgent --> Core
  12. CLIAgent --> CLI[claude CLI process]
  13. OAuthAgents --> OAuth[core/oauth]
  14. OAuth --> Keychain[go-pkg keychain]
  15. Core --> Stream[Stream event normalization]
  16. Core --> Usage[Usage normalization]
  17. Core --> Media[Image / audio interfaces]

Layers


LayerPackagesResponsibilityDependency direction
ContractcoreAgent interfaces, message and usage types, reasoning levels, modes, model classification, SSE normalization, optional multimodal interfacesdepends on no provider
Assemblycore/routerBuilds an Agent from provider@model; rewrites unknown prefixes to compatdepends on core and every provider
Adapterscore/claude, core/claudeCode, core/openai, core/gemini, core/grok, core/deepseek, core/mistral, core/nvidia, core/ollamaCloud, core/openRouter, core/cloudflare, core/compat, core/copilot, core/openaiCodex, core/grokOauthMessage conversion, wire-format differences, model listings, optional capabilitiesdepend on core and the shared wire helpers
Shared wire helperscore/copilot/response, core/xaiResponses API input/tool/output conversion; xAI Responses request body and SSE assemblydepend on core; core/xai also uses core/copilot/response
Credentialscore/oauth/copilot, core/oauth/codex, core/oauth/grokLogin flows, token storage and refreshdepend on core and the go-pkg keychain
Utilitycore/gemini/summaryVoiceReply: rewrites text over 320 characters into a spoken summary via Gemini, key read from the keychain GEMINI_API_KEYdepends on go-pkg only

Among other tools that call many LLM APIs, LiteLLM, Bifrost, and GoModel are positioned as AI gateways deployed as a separate service, while go-llm-router is a library imported into your Go program with no proxy in between. OpenRouter is a hosted unified interface; here each provider is called directly with its own API key or OAuth token, and openrouter is itself one of the 15 provider prefixes. LangChainGo, Eino, and Genkit are application frameworks for chains, agents, and RAG; this project covers only the provider call layer and is used on its own. go-openai and openai-go focus on the OpenAI API, whereas one Agent interface here switches across 15 provider prefixes. Alongside gollm, GoAI, Jetify AI, and the Python library any-llm, which also wrap multiple providers, go-llm-router brings provider@model string routing, a driver for the local Claude Code CLI, Copilot / Codex / Grok OAuth with keychain storage, quota and balance queries, and usage normalization that keeps cache tokens.

Adapter groups


GroupPackagesWire format
OpenAI-compatibleopenai, deepseek, mistral, nvidia, openRouter, ollamaCloud, compatChat Completions; newer OpenAI generations switch to Responses
CloudflarecloudflareWorkers AI /ai/run with a {model, input} body sent through an AI Gateway (cf-aig-gateway-id); no streaming
AnthropicclaudeMessages API; budget_tokens thinking on 4.5 / 4.1 models, adaptive thinking plus output_config.effort on newer ones
Googlegemini:generateContent with raw JSON Schema tools (parametersJsonSchema) and cachedContents prefix caching
xAIgrok, grokOauthResponses API built by core/xai, plus the image endpoints
OAuth proxiescopilot, openaiCodexVendor-internal endpoints requiring a session token: copilot uses Responses whenever the model's supported_endpoints lists /responses, Chat Completions when only /chat/completions is listed, and ResponsesAPI when neither is, openaiCodex always uses the ChatGPT backend Responses endpoint
Local CLIclaudeCodeclaude -p stream-JSON over stdin/stdout, one process per session, text-protocol tool calls

Cross-cutting principles


PrincipleWhat it means
One-way dependenciesAdapters depend on core and the shared wire helpers (core/copilot/response for openai, codex, copilot; core/xai for both Grok adapters). Two adapter-to-adapter exceptions: grokOauth reuses grok.RequestImage, openaiCodex reuses the openai image helpers
Capabilities are interfacesStreaming, reasoning limits, image, STT, and TTS are optional interfaces rather than flags or config fields
Errors carry statusSend returns the upstream HTTP status; stream failures wrap into *llmrouter.StreamError keeping Provider / Code / Body
Read caps live in the contract layer64 MiB non-streamed audio body, 8 KiB error body, 512-byte error frame, 64 KiB JSON body, all defined once in core
Shared HTTP client and timingllmrouter.NewHTTPClient gives a 15-minute timeout; NewStreamHTTPClient adds a 15-second response-header timeout for codex, grok-oauth, and OAuth requests; timeouts, poll backoff, and expiry margins are core constants shared by every adapter

Further reading


中文