Source Code:
src/gaia/api/Components: OpenAI-compatible API Server, Schemas, Endpoints, SSE Streaming
Module:
gaia.api
Import: from gaia.api import app, schemas, agent_registryOverview
The GAIA API Server provides an OpenAI-compatible REST API for exposing GAIA agents to external tools like VSCode, Claude Dev, and custom applications. It implements the OpenAI chat completions interface with streaming support, agent registration, and comprehensive debugging features. Key Features:- OpenAI-compatible endpoints (/v1/chat/completions, /v1/models)
- Server-Sent Events (SSE) streaming
- Dynamic agent registry
- Workspace root extraction from GitHub Copilot
- Debug modes (logging, prompts, step-through)
- CORS support
- Health checks
- Token usage tracking
Requirements
Functional Requirements
Core Endpoints
-
POST /v1/chat/completions
- Non-streaming responses
- SSE streaming responses
- Message history: prior turns are loaded into the agent’s conversation,
and
system/developermessages are added to its system prompt temperature,top_p, andmax_tokensapply to every model call the agent makes for the request- Tool calls support (future)
-
GET /v1/models
- List available agents as models
- Model metadata (tokens, description)
-
GET /health
- Reports API, Lemonade LLM, and RAG component status
- Returns 200 OK while the API process is serving
- Uses top-level
degradedwhen Lemonade is unavailable or no LLM is loaded
Agent Registry
-
Agent Registration
- Dynamic agent discovery
- Model ID mapping
- Agent instantiation
- Workspace configuration
-
Agent Management
- Singleton instances per agent type
- Silent mode operation
- Workspace root injection
Request Processing
-
Message Handling
- OpenAI message format;
contentis a string or an array of content parts - Text parts are joined with newlines; any other part type (
image_url,input_audio,file) is rejected with 400 systemanddevelopermessages are appended to the agent’s system prompt- The last message must be from the user (400 otherwise); earlier user/assistant/tool messages become the agent’s conversation history
- OpenAI message format;
-
Response Generation
- Agent process_query() integration
- Token counting
- Usage statistics
- Unique response IDs
Streaming Support
- SSE Streaming
- Chunk-based streaming
- Delta content format
- Finish reason reporting
- Proper SSE formatting (data: prefix)
- A client disconnect stops the agent (its cancel event and the SSE
handler’s
cancelledflag are set), so it stops running tools and generating - If the agent raises mid-stream, the stream ends with an
{"error": {...}}chunk followed bydata: [DONE]
Non-Functional Requirements
-
Performance
- Low latency response
- Efficient streaming
- Connection pooling
-
Reliability
- Error handling
- Connection recovery
- Graceful degradation
-
Debugging
- Request/response logging
- Prompt display
- Step-through mode
- Raw HTTP logging
-
Security
- CORS configuration
- Input validation
- Error sanitization
- Confirmation-gated tools (shell, file writes, MCP writes) are refused —
the API has no approval channel. See
SDK → API Server for the
GAIA_AUTO_APPROVE_TOOLSopt-out.
API Specification
File Locations
Schemas (Pydantic Models)
API Endpoints
Agent Registry
The API-server’sAgentRegistry is not a runtime plug-in registry — it is
a hardcoded AGENT_MODELS dict in src/gaia/api/agent_registry.py plus a
dynamic import helper. There is no register_agent() method today; to add a
new model you edit AGENT_MODELS and restart the server.
For the richer agent registry used by the Agent UI (not the
OpenAI-style API server), see the
Agent Registry spec covering
src/gaia/agents/registry.py and ~/.gaia/agents/ discovery of Python
custom agents.Implementation Details
Request Processing Flow
Debug Middleware
Testing Requirements
Unit Tests
File:tests/api/test_api_server.py
Usage Examples
Example 1: Start API Server
Example 2: Non-Streaming Request (curl)
Example 3: Streaming Request (curl)
Example 4: Python Client
Example 5: OpenAI Python SDK
CLI Interface
Commands
API Server Technical Specification
VS Code client authentication
Run GAIA: Set API Key from the VS Code command palette and enter the same value as the server’sGAIA_API_KEY. The extension stores it in VS Code SecretStorage and
uses it for model discovery and chat completions. Leave the input blank to clear
it; cancelling preserves the existing key. Keyless loopback servers continue to work.