Skip to main content

Overview

The Genie Chat API provides a Server-Sent Events (SSE) streaming interface to the AnythingLLM workspace agent. Unlike the embed API, this proxies to the full workspace agent mode with access to all 28 MCP tools (Directus, Ollama, Stagehand browser automation). Base Path: /api/genie Key Features:
  • Real-time streaming responses (SSE)
  • Full MCP tool integration (Directus queries, browser automation, etc.)
  • Onboarding gate enforcement (content generation blocked until setup complete)
  • Automatic persona context injection from user’s RAG node graph
  • Session-based conversation history

POST /api/genie/stream-chat

Stream AI agent responses using AnythingLLM workspace API. Authentication: Directus JWT (Bearer token)

Request Parameters

message
string
required
User’s message/prompt to the AI agent. Can include:
  • Content generation requests (“Write an Instagram caption about…”)
  • Questions (“What are my top performing posts?”)
  • Commands (“Schedule a post for tomorrow at 9am”)
sessionId
string
Optional session identifier for conversation continuity. If omitted, defaults to genie-{userId} for user-specific history.

Response Format (SSE Stream)

The endpoint returns a Server-Sent Events stream with Content-Type: text/event-stream. Event Structure:
Event Types:
type
string
Event type:
  • textResponseChunk - Incremental text chunk
  • toolCall - MCP tool invocation notification
  • error - Error occurred
textResponse
string
Text content (for textResponseChunk events)
close
boolean
true if this is the final event in the stream
error
boolean
true if an error occurred

Example Request (curl)

Response (SSE Stream):

Example Request (JavaScript Fetch)

Example Request (EventSource - Browser)

EventSource does not support POST requests or custom headers. Use fetch with streaming reader instead.
For GET-compatible SSE endpoints, use:

Onboarding Gate

The endpoint enforces an onboarding completion gate before allowing content generation: From genieChat.js:50:
Gate Response (Before Onboarding):
Gate Response (Processing Data):

Persona Context Injection

The API automatically enriches user messages with persona node context from the taxonomy graph: From genieChat.js:68:
Example Enriched Message:
This context helps the AI agent generate on-brand, personalized content matching the creator’s established voice.

Session Management

Conversations are session-isolated to maintain context across multiple turns: Default Session ID:
Use Cases:
  1. User-wide session: Omit sessionId for a single persistent conversation per user
  2. Project-specific sessions: Use custom IDs like campaign-summer-2026 for isolated planning contexts
  3. Ephemeral sessions: Generate random IDs for one-off requests without history pollution
Session Example:

MCP Tool Integration

The agent has access to 28 MCP tools via the AnythingLLM workspace: Available Tool Categories:
  • directus_query_items - Read from any Directus collection
  • directus_create_item - Create records
  • directus_update_item - Update records
  • directus_delete_item - Delete records
Example Use: “What are my top 5 most engaged Instagram posts from last month?”
  • ollama_generate - Text generation with any local model
  • ollama_chat - Multi-turn chat completions
Example Use: “Generate 10 caption variations for this topic”
  • stagehand_navigate - Navigate to URL
  • stagehand_extract - Extract page data
  • stagehand_click - Click elements
  • stagehand_type - Input text
Example Use: “Check my Instagram analytics and summarize engagement trends”
  • instagram_get_insights - Fetch Instagram analytics
  • tiktok_get_videos - List TikTok videos
  • youtube_get_analytics - YouTube channel stats
Example Use: “Which of my YouTube videos has the best retention rate?”
Tool Call Event Example:

Error Handling

Unauthorized (no token):
Invalid/expired token:
Missing message:
Upstream AnythingLLM error:
SSE Stream Error Event:

Admin Endpoint: Taxonomy Rebuild

POST /api/genie/taxonomy/rebuild-graph

Apply human-reviewed taxonomy assignments to the knowledge graph and re-upload to AnythingLLM RAG. Authentication: Directus JWT (admin users only) Authorization Check:
Response:
The rebuild runs asynchronously:
  1. Fetches all taxonomy_review_queue items with status=assigned
  2. Adds MAPS_TO_CONCEPT edges to taxonomy_graph.json
  3. Saves updated graph
  4. Re-uploads RAG document to AnythingLLM (persona context)
Use Case: After human reviewers assign content nodes to taxonomy concepts in the admin UI:
Logs:

Configuration

Environment Variables

Workspace Configuration

Ensure the target workspace has:
  1. Agent mode enabled (not chat/query mode)
  2. MCP servers configured (Directus, Ollama, Stagehand)
  3. System prompt tuned for creator content generation
  4. RAG documents loaded (persona nodes, taxonomy graph)

Performance Considerations

Streaming Latency:
  • First token: 300-800ms (model warm-up + context processing)
  • Token throughput: 20-50 tokens/sec (depends on model size)
  • Network overhead: Negligible with HTTP/2 + keep-alive
Connection Timeouts: SSE streams can stay open indefinitely. Set client-side timeouts:
Nginx Buffering: Disable buffering for SSE endpoints:
The API already sets X-Accel-Buffering: no header (line 102).

React Integration Example


Next Steps

Captions API

Non-streaming caption generation endpoint

Queue API

Enqueue long-running AI tasks as background jobs