Full technical details in your project at
/docs/modules/AI.mdWhat You Get
- ✅ Proxy architecture - API keys server-side only (secure)
- ✅ Streaming SSE - Real-time token-by-token responses
- ✅ Environment-gated - Auto-selects Proxy or Echo client
- ✅ 500+ models - OpenAI, Anthropic, Google, Meta, DeepSeek
- ✅ Cancellation support - Stop generation mid-stream
- ✅ Echo fallback - Test UI without API costs
Key Components
LLMClient Protocol
Production Setup (From Real Code)
Here’s how the boilerplate actually creates the LLM client inCompositionRoot.swift:
Why This Architecture
Proxy Pattern (Production):- ✅ API keys never in app (stored in Supabase secrets)
- ✅ Authentication via JWT (automatic via AuthInterceptor)
- ✅ Backend controls costs and rate limits
- ✅ Can switch models without app update
- ✅ Test UI without backend setup
- ✅ No API costs during development
- ✅ Perfect for rapid iteration
- ✅ Auto-enabled when PROXY_BASE_URL empty
Streaming Pattern
- ✅ Low latency (first token quickly)
- ✅ Better UX (gradual appearance)
- ✅ Cancellable (stop generation)
- ✅ Memory efficient
Supported Models
Access any model from openrouter.ai/models:OpenAI
openai/gpt-4o($2/1M tokens)openai/gpt-4o-mini($0.12/1M tokens)
Anthropic
anthropic/claude-3.7-sonnet($2.50/1M)anthropic/claude-3-5-haiku($0.50/1M)
google/gemini-2.5-pro($1.25/1M)google/gemini-2.0-flash($0.40/1M)
Meta
meta-llama/llama-3.3-70b($0.60/1M)- Open source, cost-effective
Supabase Edge Function
The proxy keeps API keys secure:Customization Examples
Change AI Model
Add System Prompt
Add Direct LLM Provider
Security
API keys never in client:- ✅ Edge Function holds OpenRouter key
- ✅ User auth required for proxy access
- ✅ Rate limiting at Edge Function
- ✅ No keys in client code
- ✅ Messages not logged by proxy
Key Files
| Component | Location |
|---|---|
| Protocol | Packages/AI/Sources/AI/LLMClient.swift |
| Proxy Client | Packages/AI/Sources/AI/Clients/ProxyLLMClient.swift |
| Echo Client | Packages/AI/Sources/AI/Clients/EchoLLMClient.swift |
| Edge Function | supabase/functions/ai/index.ts |
Dependencies
- Core - Error handling, logging
- Networking - HTTP client for proxy
Used By
- FeatureChat - Chat ViewModels
- Custom features - Your AI features
Best Practices
Streaming
Streaming
- Use AsyncThrowingStream
- Handle cancellation
- Update UI incrementally
- Show loading state
Error Handling
Error Handling
- Map to AppError
- Retry transient failures
- Show user-friendly messages
- Log technical details
Cost Management
Cost Management
- Choose appropriate model
- Use mini/flash for simple tasks
- Cache system prompts
- Limit message history
Learn More
Full Documentation
Complete AI guide
Supabase Setup
Deploy Edge Function
Feature Chat
See AI integration
Building Guide
Customize AI experience
Test Coverage
91%+ - Comprehensive testing Tests include:- Streaming responses
- Error handling
- Cancellation
- Model selection
- Timeout handling
