AI Package
@cdoing/ai — The intelligence layer containing the agentic loop, LLM providers, system prompt builder, and context management.
Overview
The AI package is the brain of the system. It manages the continuous cycle of LLM inference and tool execution that makes the agent work autonomously.
Agent Runner
The agent runner implements the core agentic loop. It's the most critical component in the system.
How the Loop Works
Key Features
- Real-time Streaming — Token-by-token output via LangChain's .stream() method
- Parallel Tool Execution — Multiple independent tool calls can run concurrently
- Exponential Backoff Retry — Automatic retry on transient API failures
- Context Compression — Automatic summarization when approaching token limits
- Raw JSON Schema Tool Binding — No Zod dependency, maximum provider compatibility
LLM Providers
The provider factory creates LLM instances for any supported provider:
| Provider | Package | Default Model |
|---|---|---|
| Anthropic | @langchain/anthropic | claude-sonnet-4-6 |
| OpenAI | @langchain/openai | gpt-4o |
| @langchain/google-genai | gemini-2.0-flash | |
| Ollama | @langchain/openai (compatible) | llama3.1 |
| Custom | @langchain/openai (OpenAI-compatible) | User-defined |
Provider Enum
OAuth Support
Anthropic OAuth is supported for Claude Pro/Max users with Bearer authentication and beta headers. The CLI provides --login and --logout commands for managing OAuth sessions.
System Prompt Builder
The system prompt is dynamically constructed based on the current state:
- Tool Definitions — Lists all available tools with their schemas
- Permission Context — Current permission mode and active rules
- Project Context — Working directory, project type, available config files
- Sandbox Status — Filesystem and network restrictions
- Custom Instructions — User-defined instructions from config files
Context Manager
The context manager handles the conversation's token budget:
Token Tracking
- Estimates tokens at ~4 characters per token
- Tracks input/output tokens via LangChain's UsageMetadata
- Triggers context pruning at 75% of the model's context window
Context Compression
- Summarizes older messages to free up context space
- Truncates large tool outputs (30k character limit, preserves tail)
- Maintains recent messages in full for continuity
Cost Tracking
The context manager calculates approximate cost per provider based on token usage and provider-specific pricing.