Overview
The/api/chat endpoint powers Maxw AI’s core conversational experience. It uses Anthropic Claude Sonnet 4.5 with native tool support, streaming responses, and extended thinking capabilities for complex reasoning tasks.
This endpoint returns streaming responses using the AI SDK’s
UIMessageStream format. Responses include text chunks, tool calls, and thinking blocks in real-time.Endpoint
Authentication
Requires a valid user session via Better-Auth. The endpoint extracts user context from session headers.Request Body
Array of conversation messages. Each message follows the AI SDK
UIMessage format with role (“user” | “assistant” | “system”) and content (string or multi-part content).Unique chat session identifier (chatId). Used for context persistence and associating messages with a conversation.
Optional trigger type for the chat interaction (e.g., “user”, “suggestion”, “auto”).
Agent Context Building
The endpoint automatically builds rich context for the AI agent on each request:AgentContext Type Definition
AgentContext Type Definition
Context Collection Process
route.ts:24-86-
Geolocation Extraction: Captures user location from Vercel request headers
-
User Authentication: Validates session and retrieves user profile
-
Canvas Integration: Loads user’s enrolled courses
-
Timezone Detection: Uses geolocation or browser defaults
AI Model Configuration
The endpoint uses Claude Sonnet 4.5 with several advanced features:Extended Thinking
Enables deep reasoning for complex queries with a 10,000 token budget:Container Skills
Persistent execution environment with document processing capabilities: route.ts:108-124Containers persist files across conversation turns and expire after ~4.5 minutes of inactivity. The
existingContainerId is extracted from the last assistant message to maintain continuity.Prompt Caching
Optimizes token usage by caching static content: route.ts:133-156Tool Execution
The agent has access to native and custom tools:Native Anthropic Tools
code_execution
Python sandbox for calculations and programmatic tool calling
web_search
Location-aware web search (uses geolocation from context)
memory
Persistent user memory with filesystem-like interface
web_fetch
Fetch and process web content from URLs
Custom Tools
Canvas LMS Tools
Canvas LMS Tools
- searchContent: Semantic search over Canvas assignments, pages, syllabus
- getClassAssignments: Fetch assignments (programmatic calling only)
Todo Management Tools (Programmatic Only)
Todo Management Tools (Programmatic Only)
- getTodos: Retrieve user’s todo list with filtering
- createTodo: Create new tasks with Canvas linking
- updateTodo: Modify existing todos
- deleteTodo: Permanently delete tasks
Study Tools
Study Tools
- createStudySet: Generate flashcards with term/definition pairs
Tools marked “programmatic calling only” are invoked from within Python code execution blocks, enabling efficient batch operations without round-trip latency.
Streaming Response
The endpoint returns a streaming response using AI SDK’stoUIMessageStreamResponse():
route.ts:158-195
Stream Format
The stream includes:- Text chunks: Streamed word-by-word for smooth rendering
- Tool calls: Real-time tool execution notifications
- Thinking blocks: Extended reasoning steps (when enabled)
- Provider metadata: Container IDs, cache metrics, token usage
Error Handling
The endpoint includes comprehensive error handling:Missing or invalid request parameters
Invalid or missing session token
Model or tool execution errors
Performance Optimizations
Cache Metrics
TheonFinish callback logs cache performance:
Smooth Streaming
Word-level chunking improves perceived responsiveness:Related Documentation
Agent Configuration
System prompt and tool configuration
Custom Tools
Canvas, Todo, and Study tool implementations
Memory System
Persistent user memory across conversations
Metadata API
Chat title and suggestion generation