Skip to content

System Architecture

Quick Reference

  • Type: Multi-agent AI coding assistant (CLI + SDK + Web Platform)
  • Stack: TypeScript, Bun, React 19, Next.js, PostgreSQL, Drizzle ORM
  • Key Modules: CLI, SDK, Agent Runtime, Common, Internal, Web
  • Deployment: npm (CLI + SDK), Vercel (Web), Docker (DB)
  • Status: Production

Codebuff is an open-source AI coding assistant that coordinates specialized agents to understand codebases and make precise edits through natural language. Unlike single-model tools, Codebuff uses a multi-agent architecture where a primary orchestrator (Base2) delegates tasks to specialized agents (Editor, Reviewer, Thinker, etc.).

The system operates in three primary modes:

  1. CLI — Interactive terminal UI for direct developer use
  2. SDK — Programmatic API for embedding agent workflows in applications
  3. Web Platform — Dashboard for account management, Agent Store, and billing
graph TB
subgraph UserLayer["User Layer"]
DEV["👨‍💻 Developer"]
APP["📱 Application"]
end
subgraph Interface["Interface Layer"]
CLI["🖥️ CLI (Terminal UI)"]
SDK_PUB["📦 SDK (npm)"]
WEB["🌐 Web Dashboard"]
end
subgraph Core["Core Engine"]
AGENT_RT["⚙️ Agent Runtime"]
COMMON["📚 Common Library"]
AGENTS["🤖 Agent Definitions"]
end
subgraph Services["Backend Services"]
INTERNAL["🗄️ Internal Package"]
BILLING["💳 Billing"]
CODEMAP["🗺️ Code Map"]
end
subgraph External["External Services"]
OPENROUTER["OpenRouter (LLMs)"]
STRIPE["Stripe"]
POSTHOG["PostHog"]
GITHUB["GitHub OAuth"]
end
subgraph Data["Data Layer"]
PG["PostgreSQL"]
BQ["BigQuery"]
end
DEV --> CLI
APP --> SDK_PUB
DEV --> WEB
CLI --> AGENT_RT
SDK_PUB --> AGENT_RT
WEB --> INTERNAL
AGENT_RT --> AGENTS
AGENT_RT --> COMMON
AGENT_RT --> OPENROUTER
INTERNAL --> PG
INTERNAL --> STRIPE
INTERNAL --> POSTHOG
INTERNAL --> GITHUB
BILLING --> STRIPE
CODEMAP --> AGENT_RT
ComponentPackageDescriptionTechnologyKey Files
CLI@codebuff/cliTerminal user interfaceOpenTUI, React 19, Commander, Zustandcli/src/index.tsx, cli/src/chat.tsx
SDK@codebuff/sdkPublished npm package for programmatic useVercel AI SDK, tree-sitter, websocketssdk/src/client.ts, sdk/src/run.ts
Agent Runtime@codebuff/agent-runtimeAgent execution engineTool executor, LLM API, stream parsingpackages/agent-runtime/src/run-agent-step.ts
Common@codebuff/commonShared types, tools, utilitiesZod schemas, tool definitionscommon/src/tools/list.ts
Internal@codebuff/internalBackend servicesDrizzle ORM, NextAuth, OpenRouterpackages/internal/src/db/schema.ts
Web@codebuff/webWeb dashboardNext.js App Router, Tailwindweb/src/app/
Agents@codebuff/agentsAgent definitionsTypeScript agent configsagents/base2/base2.ts
Billing@codebuff/billingPayment processingStripe SDKpackages/billing/
sequenceDiagram
participant U as User
participant CLI as CLI/SDK
participant RT as Agent Runtime
participant B2 as Base2 Agent
participant LLM as LLM (OpenRouter)
participant T as Tool Executor
participant FS as File System
U->>CLI: Natural language prompt
CLI->>RT: Start agent run
RT->>B2: Initialize with context
loop Agent Steps
B2->>LLM: Send prompt + context
LLM-->>B2: Tool calls (streaming)
B2->>T: Execute tool
alt File Operation
T->>FS: read/write/search
FS-->>T: Results
else Spawn Sub-Agent
T->>RT: Spawn (Editor/Reviewer/etc.)
RT-->>T: Agent result
else Terminal Command
T->>FS: Execute command
FS-->>T: Output
end
T-->>B2: Tool result
end
B2-->>RT: Task completed
RT-->>CLI: Final result
CLI-->>U: Display changes
#DecisionContextStatus
1Multi-agent over single-agentBetter specialization, context management, and error recoveryAccepted
2Bun as runtimeFaster startup, built-in TypeScript, test runner, package managerAccepted
3OpenRouter for LLM routingAccess any model (Claude, GPT, DeepSeek, Qwen) via single APIAccepted
4OpenTUI for CLI renderingReact-based terminal rendering with Yoga layoutAccepted
5Drizzle ORM over PrismaType-safe, lightweight, SQL-first approachAccepted
6tree-sitter for code parsingLanguage-agnostic AST analysis in SDKAccepted
7Credit-based billingFlexible usage tracking across tiers and organizationsAccepted
ADR-001: Multi-Agent Architecture

Context: AI coding assistants typically use a single model for all tasks. This creates context window limitations and reduces accuracy for specialized operations.

Decision: Implement a multi-agent system where a primary orchestrator (Base2) delegates to specialized agents (Editor, File Explorer, Reviewer, Thinker, Researcher).

Consequences:

  • ✅ Better accuracy through agent specialization
  • ✅ Independent context windows per agent
  • ✅ Parallel execution possible
  • ⚠️ More complex orchestration logic
  • ⚠️ Higher total token usage per task
ADR-002: Bun Runtime

Context: The project needed a fast TypeScript runtime with built-in tooling to reduce dependency on external build tools.

Decision: Use Bun as the primary runtime, package manager, test runner, and bundler.

Consequences:

  • ✅ 30-40x faster package installs
  • ✅ Built-in TypeScript execution (no compilation step)
  • ✅ Native test runner (no Jest dependency for CLI/SDK)
  • ⚠️ Ecosystem compatibility issues with some npm packages
  • ⚠️ SDK must still support Node.js ≥18 for consumers
LayerMechanismDetails
AuthenticationNextAuth + GitHub OAuthWeb sessions, PATs, CLI tokens
API KeysEncrypted at restencrypted_api_keys table with per-user encryption
Session TypesTyped sessionsweb, pat, cli — different trust levels
OrganizationRBACowner, admin, member roles per org
Rate LimitingCredit-basedPer-block and weekly limits with configurable overrides
FingerprintingDevice trackingSession-fingerprint binding for abuse prevention
AspectStrategyDetails
LLM CallsOpenRouter routingAutomatic model fallback and load balancing
DatabaseConnection poolingpg driver with Drizzle ORM
Context ManagementContext pruningcontext-pruner.ts (~37KB) manages token window
Agent HierarchyAncestor trackingancestor_run_ids array for tracing agent trees
StreamingServer-sent eventsReal-time token streaming from LLM to CLI
AnalyticsBigQuery pipelineAsync event logging for analytics