Overview
The Agent Core module provides the central agent loop functionality, handling message processing, tool execution, and agent lifecycle management. It orchestrates LLM calls, tool invocations, and context management.Main Functions
agent::chat
Process a message through the complete agent loop with validation, security checks, tool execution, and response generation.Process Flow
The agent::chat function executes the following steps:- Request Validation - Validates agentId and checks rate limits
- Concurrent Slot Acquisition - Ensures agent doesn’t exceed concurrent request limits
- Context Preparation - Loads agent config, recalls memories, and prepares tools
- Security Scanning - Scans for injection attacks (blocks if riskScore > 0.5)
- Budget Checking - Verifies agent hasn’t exceeded token/cost budgets
- LLM Execution - Calls the LLM with messages and available tools
- Tool Loop - Executes tool calls iteratively (max 50 iterations)
- Response Recording - Stores conversation in memory and updates metrics
Constants
- MAX_ITERATIONS: 50 - Maximum tool execution iterations
- TOOL_TIMEOUT_MS: 120,000 - Tool execution timeout (2 minutes)
- MAX_CHAT_TIMEOUT_MS: 300,000 - Overall chat timeout (5 minutes)
- CONTEXT_HEALTH_CHECK_INTERVAL: 10 - Check context health every N iterations
- CONTEXT_HEALTH_THRESHOLD: 60 - Compress context if health score < threshold
agent::list_tools
List all tools available to a specific agent based on its capabilities and tool profile.The agent identifier to list tools for
Array of tool definitions with function_id, description, and metadata
agent::create
Register a new agent with configuration and capabilities.Optional agent ID (auto-generated if not provided)
Agent name
Default system prompt for the agent
Allowed tool prefixes (e.g., [“tool::”, “agent::”] or [”*”] for all)
Tool filtering profile: “full”, “minimal”, or custom
Enable automatic code detection and execution
The created agent’s unique identifier
agent::list
List all registered agents.Array of agent configurations
agent::delete
Remove an agent from the system.The agent identifier to delete
Confirmation of deletion
agent::list_by_division
List agents filtered by division (organizational grouping).Optional division filter (e.g., “engineering”, “support”)
Filtered array of agents
Internal Functions
These functions are used internally by the agent loop:validateRequest
Validates the chat request, checks rate limits, and acquires concurrent execution slots. Location:agent-core.ts:43
prepareContext
Prepares the execution context by loading agent config, recalling memories, listing tools, routing to appropriate model, and scanning for security threats. Location:agent-core.ts:95
executeLlmCall
Executes a single LLM completion call with the current messages and tools, recording replay events and tracking token costs. Location:agent-core.ts:162
executeToolCall
Executes a single tool call with comprehensive security checks including guard policies, approval requirements, capability verification, and policy enforcement. Location:agent-core.ts:291
Security Checks Performed:
- Tool allowlist verification
- Guard circuit breaker check
- Approval tier decision (sync/async)
- Policy enforcement
- Capability verification
toolLoop
Executes the iterative tool calling loop, handling multiple rounds of tool execution until the agent produces a final response or reaches MAX_ITERATIONS. Location:agent-core.ts:484
Features:
- Automatic context health monitoring
- Context compression when health degrades
- Audit logging for each iteration
- Tool result injection into message history
handleCodeAgent
Detects and executes code blocks when codeAgentMode is enabled, automatically running code and feeding results back into the conversation. Location:agent-core.ts:213
Configuration
The agent core connects to the engine via theENGINE_URL environment variable and registers as the “agent-core” worker.
Hooks and Events
The agent loop fires several hooks that can be listened to:- BeforeToolCall - Fired before each tool execution
- AfterToolCall - Fired after successful tool execution
- AgentLoopEnd - Fired when the agent loop completes
Metrics
The agent core records the following metrics:active_sessions- Current active chat sessions (gauge)tokens_used_total- Total tokens consumed (input/output)tool_execution_total- Tool execution counts by statusfunction_call_duration_ms- Call duration histogramfunction_error_total- Error counts by type