Vercel AI SDK Architecture
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The Vercel AI SDK Architecture within OS Chat serves as the core orchestration layer for multi-model AI interactions, streaming, and tool execution. It integrates Vercel AI SDK v6 (and v5 features) to provide a unified interface for over 50 language models from providers such as OpenAI, Anthropic, Google, and Meta. This architecture supports real-time streaming, multi-modal capabilities (text, image, and reasoning), and autonomous background agents.
The system utilizes a gateway pattern and specialized transport layers, such as WebSocket fetches for OpenAI models, to optimize performance and reliability. It also handles complex logic for tool calling, reasoning effort configuration, and personalized system prompt construction based on user profiles and connected services.
Sources: CLAUDE.md:121-131, README.md:38-48, src/routes/api.chat.ts:25-50
Core Components and Data Flow
The architecture is centered around the /api/chat route and Convex internal actions, which manage the lifecycle of an AI request from user input to streamed response and final database persistence.
Chat Request Lifecycle
When a user sends a message, the system follows a structured pipeline:
- Middleware & Validation: Authentication is verified via
authMiddleware, and input is sanitized. - Configuration Retrieval: The system fetches user-specific API keys, premium status, and connected service "connectors" from the Convex backend.
- Prompt Construction: A dynamic system prompt is built using
buildSystemPrompt, incorporating user traits, timezone data, and available tool descriptions. - Model Execution: The
streamTextfunction is invoked with specialized provider options (e.g.,thinkingConfigfor Google orreasoningEffortfor OpenAI). - Streaming & Persistence: UI message streams are created and piped back to the client while the final results and token usage are saved to Convex.
Model and Provider Integration
The architecture abstracts various AI providers into a standard configuration through the MODELS_MAP and specialized builder functions.
Reasoning Effort Configuration
The system defines granular control over "thinking" or "reasoning" models using a centralized configuration. This adjusts parameters like thinkingBudget for Google/Anthropic or reasoningEffort for OpenAI.
| Effort Level | Tokens (Google/Anthropic) | Effort (OpenAI/OpenRouter) |
|---|---|---|
low | 1024 | low |
medium | 6000 | medium |
high | 12,000 | high |
Sources: src/routes/api.chat.ts:109-125
Provider-Specific Options
The SDK implementation uses specialized builders to inject provider-specific parameters:
- Google: Uses
thinkingConfigto enable thoughts and set a budget. - OpenAI: Uses
reasoningEffortandreasoningSummary. - Anthropic: Uses
thinkingwithbudgetTokens.
Sources: src/routes/api.chat.ts:153-206
Tool and Agent Framework
The architecture utilizes the ToolLoopAgent and custom tool definitions to enable external service interaction and background automation.
Tool Execution Flow
- create_agent Tool: A specialized tool that allows the AI to delegate tasks to autonomous agents. It accepts
toolslugs, atask, andcontext. - Metered Search: A search tool wrapped in credit metering to track and limit user consumption of search APIs (Exa, Tavily, Brave).
- Composio Integration: Connects the AI SDK to 10+ services like Gmail, Calendar, and GitHub.
Autonomous Scheduled Agents
For background tasks, the system uses createScheduledAgent within Convex internal actions. These agents run independently of a user session, utilizing Redis-backed ScheduledAgentMemory to maintain history across recurring executions.
Sources: convex/scheduled_ai.ts:20-45, convex/scheduled_ai.ts:210-230
Error Handling and Detection
A critical part of the architecture is the ability to detect and classify errors that occur during the streaming process.
Provider Error Detection
The system uses detectProviderErrorFromObject and detectProviderErrorInText to catch specific failure patterns (e.g., safety filters, rate limits, or context window issues) directly from the stream. This allows the system to save user-friendly error messages to the chat history instead of failing silently.
Sources: src/routes/api.chat.ts:600-630, src/routes/api.chat.ts:646-680
Conclusion
The Vercel AI SDK Architecture in OS Chat provides a robust, multi-modal framework that bridges the gap between various AI providers and a real-time React application. By combining flexible provider options, a sophisticated tool-calling system, and autonomous background capabilities, the architecture enables the platform to function not just as a chat interface, but as a proactive personal assistant capable of complex service orchestration and long-running tasks.
Sources: CLAUDE.md:121-140, README.md:131-150