This document provides a high-level overview of Cairo Coder's architecture for developers and maintainers.
Cairo Coder is an AI-powered code generation service for the Cairo programming language. It uses Retrieval-Augmented Generation (RAG) to transform natural language requests into functional Cairo smart contracts and programs.
| Component | Technology | Purpose |
|---|---|---|
| Backend | Python 3.10+ with FastAPI | API server and RAG pipeline |
| RAG Framework | DSPy | Structured prompt engineering and optimization |
| Ingester | TypeScript/Bun | Documentation processing |
| Database | PostgreSQL + pgvector | Vector similarity search |
| LLM Providers | OpenAI, Anthropic, Google Gemini | Code generation |
┌─────────────────────┐
│ Documentation │
│ Sources │
│ (Cairo Book, etc.) │
└──────────┬──────────┘
│
▼
┌─────────────────┐ ┌─────────────────────────┐
│ Ingester │──────────────│ PostgreSQL │
│ (TypeScript) │ embed & │ + pgvector │
│ │ store │ │
└─────────────────┘ └────────────┬────────────┘
│
│ retrieve
▼
┌─────────────────┐ ┌─────────────────────────┐
│ Client │──────────────│ Python Backend │
│ (API User) │ HTTP/SSE │ (FastAPI + DSPy) │
└─────────────────┘ └────────────┬────────────┘
│
│ generate
▼
┌─────────────────────────┐
│ LLM Provider │
│ (OpenAI/Anthropic/etc.) │
└─────────────────────────┘
The core RAG pipeline consists of three stages:
┌──────────────┐ ┌──────────────────┐ ┌────────────────┐
│ Query │────▶│ Document │────▶│ Generation │
│ Processing │ │ Retrieval │ │ Program │
└──────────────┘ └──────────────────┘ └────────────────┘
│ │ │
▼ ▼ ▼
Extract search Query pgvector Generate Cairo
terms & identify for similar docs code with context
relevant sources
- Analyzes user queries using DSPy's
ChainOfThought - Extracts semantic search queries
- Identifies relevant documentation sources
- Detects if query is contract/test related
- Custom
SourceFilteredPgVectorRMextends DSPy's retriever - Queries PostgreSQL with pgvector for cosine similarity search
- Filters by documentation source
- Returns documents with metadata (title, URL, source)
- Uses
CairoCodeGenerationDSPy signature - Generates Cairo code with explanations
- Supports streaming via async generators
- MCP mode returns raw documentation without LLM synthesis
cairo-coder/
├── python/ # Python backend
│ ├── src/
│ │ ├── cairo_coder/
│ │ │ ├── agents/ # Agent registry
│ │ │ ├── core/ # Core types, config, RAG pipeline
│ │ │ ├── dspy/ # DSPy modules (query, retrieval, generation)
│ │ │ └── server/ # FastAPI application
│ │ └── scripts/ # CLI utilities
│ ├── tests/ # Test suite
│ └── optimizers/ # DSPy optimization notebooks
│
├── ingesters/ # TypeScript ingester
│ ├── src/
│ │ ├── ingesters/ # Source-specific ingesters
│ │ ├── db/ # Vector store operations
│ │ ├── config/ # Settings
│ │ ├── types/ # TypeScript types
│ │ └── utils/ # Utilities (paths, markdown splitting)
│ └── __tests__/ # Bun tests
│
├── docs/ # Documentation
└── docker-compose.yml # Container orchestration
Agents are lightweight configurations that customize the RAG pipeline:
# python/src/cairo_coder/agents/registry.py
class AgentId(str, Enum):
CAIRO_CODER = "cairo-coder"
STARKNET = "starknet-agent"
registry: dict[AgentId, AgentSpec] = {
AgentId.CAIRO_CODER: AgentSpec(
name="Cairo Coder",
description="General Cairo programming assistant",
sources=list(DocumentSource), # All sources
# ...
),
}Each agent specifies:
- Name and description
- Documentation sources to search
- Pipeline builder with customizations
- Retrieval parameters (max sources, similarity threshold)
The ingester downloads, chunks, and embeds documentation:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Download & │────▶│ Chunk with │────▶│ Embed & │
│ Extract Docs │ │ Metadata │ │ Store │
└─────────────────┘ └─────────────────┘ └─────────────────┘
- Template Method Pattern (
BaseIngester): Abstract class defining ingestion workflow - Factory Pattern (
IngesterFactory): Creates appropriate ingester per source
- Create ingester extending
BaseIngesteriningesters/src/ingesters/ - Implement abstract methods:
downloadAndExtractDocs()- Fetch documentationcreateChunks()- Split into searchable chunksgetExtractDir()- Temp storage locationparsePage()- Parse content into sections
- Register in
IngesterFactory.createIngester() - Add to
DocumentSourceenum in both:ingesters/src/types/index.tspython/src/cairo_coder/core/types.py
- Update resource descriptions in
python/src/cairo_coder/dspy/query_processor.py
PostgreSQL with pgvector extension stores document embeddings:
| Column | Type | Description |
|---|---|---|
id |
UUID | Primary key |
content |
TEXT | Document text |
embedding |
VECTOR(1536) | OpenAI text-embedding-3-large |
metadata |
JSONB | title, sourceLink, source, uniqueId, etc. |
Similarity search uses cosine distance:
SELECT * FROM documents
ORDER BY embedding <=> $query_embedding
LIMIT 10;DSPy enables automatic prompt optimization:
- Dataset: Generated from Starklings exercises
- Metrics: Code compilation success, relevance scores
- Optimizer: MIPROv2 for few-shot prompt tuning
- Notebooks: Marimo reactive notebooks with MLflow tracking
Optimized programs are saved to python/optimizers/results/ and loaded in production.
All configuration via environment variables (.env in root):
Required:
POSTGRES_*- Database credentialsOPENAI_API_KEY- For embeddings (required) and LLM (optional)
Optional:
ANTHROPIC_API_KEY- Anthropic Claude modelsGEMINI_API_KEY- Google Gemini modelsLANGSMITH_API_KEY- Tracing/observabilityXAI_API_KEY- Grok search functionality