/api/chat endpoint combines vector search with LLM generation to provide context-aware responses based on uploaded documents.
Overview
The chat endpoint performs:- Vector Search - Retrieves relevant document chunks (if sources not provided)
- Context Building - Constructs RAG context from retrieved sources
- LLM Generation - Calls LLM provider (Workers AI, OpenAI, Anthropic, Google)
- Response Formatting - Returns response with sources and metadata
Endpoint
POST /api/chat
Request Parameters
query (required)
The user’s question or prompt.agentType (optional)
Predefined agent type for automatic prompt selection. Available types:rag-chat- General RAG assistantgraph-analyst- Knowledge graph analysisexecutive-summary- Executive summariestechnical-auditor- Technical analysisfuture-planner- Strategic planningcoordinator- Idea synthesiscritic- Critical analysis
sources (optional)
Pre-retrieved sources. If not provided, the endpoint performs vector search automatically.llmConfig (optional)
LLM provider and model configuration. Defaults to Workers AI if not specified.workers-ai- Cloudflare Workers AI (default, no API key required)google- Google Gemini (requiresGEMINI_API_KEY)openai- OpenAI (requiresOPENAI_API_KEY)anthropic- Anthropic Claude (requiresANTHROPIC_API_KEY)
systemPrompt (optional)
Custom system prompt. OverridesagentType if provided.
temperature (optional)
Controls response creativity. Default:0.2 (more deterministic).
0.0- Very deterministic0.7- Balanced1.0- More creative
historyContext (optional)
Previous conversation context for continuity.userGoals (optional)
User goals for personalized responses.Rate Limiting
- Limit: 10 requests per minute per user
- Headers:
X-RateLimit-Remaining- Remaining requestsX-RateLimit-Reset- Reset timestamp
- Status:
429when limit exceeded
Example Requests
Basic Chat
With Agent Type
With Custom LLM
With Pre-retrieved Sources
Environment Setup
API Keys (Optional)
API keys are stored as Cloudflare secrets (not inwrangler.toml):
If no API keys are set, the endpoint uses Cloudflare Workers AI (free, no keys required).
Benefits
- Security - API keys never exposed to frontend
- Cost Control - Centralized rate limiting and usage tracking
- Flexibility - Easy to switch between LLM providers
- Performance - Single request for search + generation
- Consistency - All API calls go through worker
Related Documentation
- API Endpoints - Complete API reference
- Backend Worker - Worker implementation