๐ค LLM INFERENCE SYSTEM โ
Overview โ
Location: /worker/agents/inferutils/
Centralized inference engine that all operations use to call LLMs.
Key features:
- Multi-provider support (OpenAI, Anthropic via Cloudflare AI Gateway)
- Streaming responses
- Tool calling with recursive execution
- Retry logic with exponential backoff
- Cancellation support (AbortController)
- Token tracking
Inference Flow โ
Operation (PhaseImplementation, UserConversationProcessor, etc.)
โ
getOperationOptions() โ InferenceContext
โ
executeInference(args, context)
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Retry Loop (max 3 attempts) โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ
infer(args)
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ OpenAI SDK (via AI Gateway) โ
โ - Model selection โ
โ - Token streaming โ
โ - Tool call parsing โ
โโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
โ
Tool calls present?
โ
โโโโโโโโโดโโโโโโโโ
Yes No
โ โ
โ โ
Execute tools Return response
Recursive inferModel Selection โ
Location: /worker/agents/inferutils/config.ts
Available models:
- GPT-4o - Fast, good for most tasks
- GPT-4o-mini - Cheapest, simple operations
- Claude 3.5 Sonnet - Best for complex reasoning
- Gemini 2.0 Flash - Fast, experimental
- Gemini 2.5 Pro - Highest quality, deep debugging
Selection by operation:
- Blueprint generation: GPT-4o
- Phase planning: GPT-4o
- File generation: GPT-4o
- Conversation: GPT-4o-mini
- Deep debugging: Gemini 2.5 Pro (reasoning_effort: high)
- Code review: GPT-4o
Streaming โ
When enabled:
- User conversation responses
- Deep debugger output
- Real-time code generation feedback
How it works:
- LLM sends Server-Sent Events (SSE)
infer()yields chunks via async generator- Operation accumulates + forwards to WebSocket
- Frontend renders progressively
Tool Calling โ
Recursive execution:
- LLM response includes
tool_callsarray infer()executes each tool in parallel- Results collected
- Filtered (empty/null results skipped)
- If results exist: call LLM again with tool outputs
- Repeat until LLM provides final response
Max depth: Configurable per operation
Retry Logic โ
Triggers retry:
- Rate limit errors (429)
- Network timeouts
- Temporary API failures (5xx)
Does NOT retry:
- Cancelled operations (AbortError)
- Invalid API key (401)
- Malformed requests (400)
Backoff: Exponential (1s, 2s, 4s)
Cancellation โ
Each operation gets AbortSignal:
User clicks stop button
โ
WebSocket: STOP_GENERATION
โ
agent.cancelCurrentInference()
โ
AbortController.abort()
โ
OpenAI SDK cancels HTTP request
โ
infer() throws InferError('cancelled')
โ
No retry, immediate propagationNested operations: Share same AbortController Tool calls: All cancelled together