Skip to main content
The /api/chat endpoint combines vector search with LLM generation to provide context-aware responses based on uploaded documents.

Overview

The chat endpoint performs:
  1. Vector Search - Retrieves relevant document chunks (if sources not provided)
  2. Context Building - Constructs RAG context from retrieved sources
  3. LLM Generation - Calls LLM provider (Workers AI, OpenAI, Anthropic, Google)
  4. 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 assistant
  • graph-analyst - Knowledge graph analysis
  • executive-summary - Executive summaries
  • technical-auditor - Technical analysis
  • future-planner - Strategic planning
  • coordinator - Idea synthesis
  • critic - 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.
Supported Providers:
  • workers-ai - Cloudflare Workers AI (default, no API key required)
  • google - Google Gemini (requires GEMINI_API_KEY)
  • openai - OpenAI (requires OPENAI_API_KEY)
  • anthropic - Anthropic Claude (requires ANTHROPIC_API_KEY)

systemPrompt (optional)

Custom system prompt. Overrides agentType if provided.

temperature (optional)

Controls response creativity. Default: 0.2 (more deterministic).
  • 0.0 - Very deterministic
  • 0.7 - Balanced
  • 1.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 requests
    • X-RateLimit-Reset - Reset timestamp
  • Status: 429 when 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 in wrangler.toml):
If no API keys are set, the endpoint uses Cloudflare Workers AI (free, no keys required).

Benefits

  1. Security - API keys never exposed to frontend
  2. Cost Control - Centralized rate limiting and usage tracking
  3. Flexibility - Easy to switch between LLM providers
  4. Performance - Single request for search + generation
  5. Consistency - All API calls go through worker