Documentation
¶
Overview ¶
Package llm hosts provider-neutral LLM utilities — currently the rule-based RouterProvider for multi-model selection — and the per-provider subpackages.
Concrete providers live in their own subpackages so a binary that uses one vendor does not statically link the other vendors' SDKs:
- pkg/llm/anthropic — Claude (Messages API)
- pkg/llm/openai — OpenAI + OpenAI-compatible endpoints, embedder, vision analyzer, summary provider
- pkg/llm/gemini — Google Gemini (public API + Vertex AI), embedder, media analyzer
Index ¶
- type RouteCondition
- type RouterProvider
- func (r *RouterProvider) AddRoute(condition RouteCondition, provider agent.LLMProvider) *RouterProvider
- func (r *RouterProvider) Capabilities() agent.LLMCapabilities
- func (r *RouterProvider) GenerateStream(ctx context.Context, memory []history.Message, availableTools *tools.Registry, ...) (agent.LLMResult, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RouteCondition ¶
RouteCondition inspects the current conversation and returns true when the associated provider should handle the request.
func Always ¶
func Always() RouteCondition
Always returns a RouteCondition that always matches. Use it as the last route to make a provider the "second fallback":
router.AddRoute(llm.IfTokensUnder(500), fastProvider).
AddRoute(llm.Always(), mediumProvider) // before the real fallback
func IfLastMessageContains ¶
func IfLastMessageContains(keywords ...string) RouteCondition
IfLastMessageContains returns true when the most recent user message contains any of the given keywords (case-insensitive). Use for intent-based routing:
llm.IfLastMessageContains("summarize", "tldr", "shorten") → fastProvider
llm.IfLastMessageContains("analyze", "compare", "reason") → powerfulProvider
func IfMessageCountUnder ¶
func IfMessageCountUnder(n int) RouteCondition
IfMessageCountUnder returns true when the conversation has fewer than n messages. Conversations with few turns are usually simple and can use a lighter model.
func IfSystemPromptContains ¶
func IfSystemPromptContains(keyword string) RouteCondition
IfSystemPromptContains returns true when any system message contains the keyword. Useful for routing sub-agents by their declared role:
// In a sub-agent with system prompt "You are a summarizer…"
llm.IfSystemPromptContains("summarizer") → fastProvider
func IfTokensUnder ¶
func IfTokensUnder(threshold int) RouteCondition
IfTokensUnder returns true when the estimated token count of the conversation is below threshold. Uses the same 4-chars/token heuristic as the agent loop. Useful for routing short/cheap tasks to a fast model.
type RouterProvider ¶
type RouterProvider struct {
// contains filtered or unexported fields
}
RouterProvider is an agent.LLMProvider that forwards each request to the first provider whose RouteCondition matches the conversation. Falls back to a default provider when no condition matches.
Typical pattern — use a cheap/fast model for short or simple tasks:
router := llm.NewRouterProvider(powerfulProvider).
AddRoute(llm.IfTokensUnder(500), fastProvider).
AddRoute(llm.IfLastMessageContains("summarize", "tldr"), fastProvider)
Register as the LLM for any AgentLoop:
loop := agent.NewAgentLoop(sessions, registry, router)
func NewRouterProvider ¶
func NewRouterProvider(fallback agent.LLMProvider) *RouterProvider
NewRouterProvider creates a router with the given fallback provider. The fallback is used when no route condition matches.
func (*RouterProvider) AddRoute ¶
func (r *RouterProvider) AddRoute(condition RouteCondition, provider agent.LLMProvider) *RouterProvider
AddRoute registers a condition+provider pair. Routes are evaluated in declaration order; the first match wins.
func (*RouterProvider) Capabilities ¶ added in v0.40.0
func (r *RouterProvider) Capabilities() agent.LLMCapabilities
Capabilities reports the intersection over the fallback and every route: a feature holds only when every provider the router might dispatch to supports it. The router cannot answer per-route, because the route is chosen from the conversation and is unknown until GenerateStream runs.
A member that does not implement agent.CapabilityProvider makes no claim, and the intersection counts it as supporting nothing — one undeclared member collapses the whole report to the zero value. That is deliberate: under-reporting costs a caller a spurious rejection at construction, while over-reporting costs it a wrong answer at run time with nothing in the log to distinguish it from a working call.
func (*RouterProvider) GenerateStream ¶
func (r *RouterProvider) GenerateStream( ctx context.Context, memory []history.Message, availableTools *tools.Registry, streamChan chan<- agent.StreamEvent, ) (agent.LLMResult, error)
GenerateStream selects the appropriate provider and delegates.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package anthropic implements agent.LLMProvider for Anthropic's Claude models via the Messages API, including prompt caching, adaptive thinking, and native structured output.
|
Package anthropic implements agent.LLMProvider for Anthropic's Claude models via the Messages API, including prompt caching, adaptive thinking, and native structured output. |
|
Package gemini implements agent.LLMProvider for Google Gemini — both the public Gemini API and Vertex AI — plus the Gemini-backed embedder and multimodal media analyzer.
|
Package gemini implements agent.LLMProvider for Google Gemini — both the public Gemini API and Vertex AI — plus the Gemini-backed embedder and multimodal media analyzer. |
|
Package llmfake provides in-tree fakes for the agent.LLMProvider interface, mirroring pkg/history/historyfake for the session layer.
|
Package llmfake provides in-tree fakes for the agent.LLMProvider interface, mirroring pkg/history/historyfake for the session layer. |
|
Package openai implements agent.LLMProvider for the OpenAI Chat Completions API and any OpenAI-compatible endpoint (Ollama, Groq, Together AI, vLLM, ...), plus the OpenAI-backed embedder, vision analyzer, and history summary provider.
|
Package openai implements agent.LLMProvider for the OpenAI Chat Completions API and any OpenAI-compatible endpoint (Ollama, Groq, Together AI, vLLM, ...), plus the OpenAI-backed embedder, vision analyzer, and history summary provider. |