Entry Points
How It Works (High-level)
- Request Reception — Next.js API validates identity, checks credit balance, loads LLM and tool configuration, asynchronously saves user message, forwards to Python backend
- Context Assembly —
_init_context()sequentially loads tools (MCP + OAuth + Built-in), initializes LLM, retrieves Memory/Profile, activates Skills, builds System Prompt, caches tocontext_cache - Agent Creation — Creates a LangGraph graph via DeepAgents, assembling LLM, tools, middleware pipeline, Checkpointer, and HITL interrupt configuration
- Message Construction — Frontend chat_history is converted to LangChain message types (max 30), with current user message appended
- Streaming Execution — Enters
_astream_events()core loop; framework events are converted to SSE messages and streamed - Completion — Sends
CompleteMessage; Checkpointer automatically saves state
Context Assembly
After context assembly completes, it is cached in_context_cache[session_id] for reuse during HITL resume().
Context Details
System Prompt assembly, Token management & optimization strategies
System Prompt
System prompt — CORE, SOUL, TOOLS, WORKFLOW, MEMORY, dynamic injection
Event Streaming
_astream_events() listens to DeepAgents framework internal events and converts them to standard SSE messages for the frontend:
SSE Event Types
Messages
Learn about the complete message flow, state management, and persistence
Tool Execution
Execution Decision
Approval decisions are based on Auto-Run mode (Run Everything / Use Allowlist / Ask Everytime). Tool Call IDs are matched via a FIFO queue, stored grouped by tool name; enqueued duringon_chat_model_end, dequeued during on_tool_end.
HITL Interrupt & Recovery
When a tool requires approval, the Agent Loop is suspended and state is persisted via Checkpointer. Recovery flow: Rejected tools have a SystemMessage appended, explicitly telling the Agent not to retry.HITL Details
Complete description of Auto-Run modes, approval UI, and interrupt recovery mechanism
Frontend Processing
The frontendhandleStreamMessage() consumes the SSE stream, routing events to corresponding state management:
Event Persistence
RealtimeEventSaver batch-persists real-time events:
Error Handling
Backend Errors
Errors are sent via
ErrorMessage SSE events, containing error_code and details.
Frontend Errors
Stream errors (AbortError, network disconnect, parse errors) all have corresponding exception handling and user notifications.
Timeouts
Concurrency & Isolation
- Each Session has independent Checkpointer state (
thread_idisolation) - Context Cache is isolated by
session_id; resume can only recover the corresponding session - Tool execution is serialized (LangGraph guarantees no concurrent tool execution within the same Session)
- User workspaces are fully isolated by
user_id
Where Things Can End Early
- Agent timeout — Exceeds 7200s maximum execution time
- HITL approval timeout — User does not respond within the specified time
- Frontend disconnect — Network interruption or user closes the page
- Credit exhaustion — Pre-call check fails
- Model error — Context window overflow or API exception