Services Module
110 files · ~80K linesThe Service Layer — 8 background services that power everything. API client, MCP runtime (470KB!), context compaction, LSP diagnostics, memory extraction, analytics, tool orchestration, and token management.
Service Catalogue — 8 Services
Each service is an independent subsystem with a clear input/output contract.
services/api/- ·Builds Anthropic API requests with full system prompt, tools, and message history
- ·Handles streaming token events: text_delta, tool_use start/delta/stop
- ·Token budget management — enforces context limits before sending
services/mcp/- ·MCP protocol client supporting 4 transport types: stdio, SSE, HTTP, WebSocket
- ·Fetches tool definitions from connected servers at startup
- ·Patches MCP tools directly into Claude's tool namespace at runtime
services/compact/- ·4-strategy escalating compression pipeline triggered when context approaches limit
- ·Strategies escalate from simple truncation to full AI-summarization of history
- ·Tracks compression ratios and chooses least-aggressive strategy that fits
services/lsp/- ·Language Server Protocol integration for IDE-quality diagnostics
- ·Runs linters/type checkers as background processes connected via LSP
- ·Surfaces errors inline in tool results so Claude can fix them immediately
services/extractMemories/- ·Background agent that runs after every conversation turn
- ·Sends recent messages to Claude: 'what facts are worth remembering?'
- ·Stores extracted memories as YAML in ~/.claude/memories/ for future sessions
services/analytics/- ·Async event pipeline — usage events are queued and never block the main loop
- ·Drains to Datadog metrics + first-party analytics endpoint
- ·Includes a PII safety type called SanitizedEventProperties to mark clean data
services/tools/- ·StreamingToolExecutor — queues tool_use blocks as they arrive from the stream
- ·Concurrency guard — manages parallel vs sequential tool execution
- ·Routes each tool call through validate → checkPermissions → invoke → renderResult
services/tokens/- ·Tracks token consumption across the session for reporting and budget enforcement
- ·Projects remaining token budget for the current context window
- ·Feeds into services/compact to decide when compaction should trigger
API Call Lifecycle — 7 Steps
A single API call touches multiple services in sequence.
Compaction — 4 Escalating Strategies
When context gets large, compaction triggers. Each strategy is more aggressive than the last. Strategy 4 is the nuclear option.
Drop oldest messages that exceed context budget. Simple FIFO drop — no summarization yet.
Truncate large tool outputs (file contents, bash output) to their first N tokens. Tool call is preserved.
Send older conversation turns to Claude for summarization. Compressed summary replaces raw messages.
Replace everything except the last N turns with a single summary. Nuclear option — only when critically close to limit.
MCP — 4 Transport Types
470KB of MCP client code — all behind one unified interface. Different transports for different deployment contexts.
stdioSpawns a local subprocess. Communication over stdin/stdout JSON-RPC. Most common for local tools.
SSEHTTP Server-Sent Events. The MCP server runs as an HTTP endpoint. Claude receives a stream of events.
HTTPPlain HTTP REST. Each tool call is a POST request. Stateless — no persistent connection required.
WebSocketFull duplex WebSocket. The server can push updates to Claude mid-execution.
When compaction triggers, what the loop looks like, and how API calls fit in the 7-phase cycle.
StreamingToolExecutor (services/tools) is the bridge between the query loop and the 43 tools.
The main site's services page — more on MCP, compaction strategies, and memory.