LlmClient trait with implementations for multiple LLM providers. All providers support streaming, tool calling, and automatic retry with exponential backoff.
Supported Providers
Anthropic (Claude)
Claude 3.5 Sonnet, Opus, and Haiku via Messages API
OpenAI
GPT-4, GPT-4 Turbo, and GPT-3.5 Turbo
Google Vertex AI
Gemini 1.5 Pro and Flash via Vertex AI
ZAI (智谱AI)
Chinese language models from ZhipuAI
Anthropic (Claude)
Loom’s Anthropic integration supports both API key and OAuth authentication, with account pooling for high-volume deployments.Authentication
- API Key
- OAuth (Recommended)
Account Pooling
For high-volume deployments, useAnthropicPool to manage multiple accounts with automatic failover:
RoundRobin
RoundRobin
Distributes requests evenly across all healthy accounts. Best for balanced load distribution.
LeastUsed
LeastUsed
Routes to the account with the lowest recent usage. Best for quota management.
Failover
Failover
Uses a primary account until quota exhausted, then fails over to backup accounts.
Health Monitoring
Monitor pool health via the status API:Healthy- Account is operationalQuotaExceeded- 5-hour quota exhausted, retrying after cooldownUnhealthy- Permanent authentication failure, account disabled
Configuration
OpenAI
OpenAI integration provides access to GPT models via the Chat Completions API.Configuration
Retry Configuration
All LLM clients support configurable retry with exponential backoff:Rate Limiting
OpenAI returns rate limit information in headers:Google Vertex AI (Gemini)
Vertex AI provides access to Google’s Gemini models via GCP.Authentication
Vertex AI uses Application Default Credentials (ADC):1
Set up credentials
Choose one of the following methods:Service Account (Production):Default Service Account (GCE/GKE):User Credentials (Development):
2
Configure client
Token Caching
Vertex AI automatically caches access tokens to reduce auth overhead:Available Models
gemini-1.5-pro
Best for: Complex reasoning, long context (1M tokens)Flagship model with advanced reasoning capabilities
gemini-1.5-flash
Best for: Fast responses, high throughputOptimized for speed and efficiency
gemini-1.0-pro
Best for: Production workloads, stable APIPrevious generation, highly reliable
ZAI (智谱AI)
ZAI provides Chinese language models from ZhipuAI, compatible with OpenAI’s API format.Configuration
Available Models
- glm-4-plus - Most capable model, best for complex tasks
- glm-4 - Balanced performance and cost
- glm-3-turbo - Fast responses, cost-effective
ZAI uses an OpenAI-compatible API, so the client implementation is nearly identical to
OpenAIClient with ZAI-specific endpoints.Unified LlmClient Interface
All providers implement the sameLlmClient trait for consistency:
Making Requests
Streaming Responses
Error Handling
Usage Tracking
All providers return token usage information:Best Practices
Use Streaming
Stream responses for better UX. Users see output immediately instead of waiting for the entire response.
Set Timeouts
Configure appropriate timeouts (default: 5 minutes). Long-running requests should use streaming to avoid timeouts.
Handle Rate Limits
Respect
retry-after headers and implement exponential backoff. Use account pooling for high-volume workloads.Monitor Usage
Track token usage to optimize costs. Consider caching responses for repeated queries.
For implementation details, see the source in
crates/loom-server-llm-{anthropic,openai,vertex,zai}/.