Delegation allows ProbeAgent to distribute complex tasks to specialized subagents. Subagents run in-process using the ProbeAgent SDK, providing better performance and resource efficiency.
const agent = new ProbeAgent({
path: './src',
enableDelegate: true
});
// AI can now delegate tasks automatically
await agent.answer("Analyze authentication, payments, AND notifications");
// AI may delegate each to separate subagentsWhen the AI receives a complex request with multiple distinct parts, it can delegate each part to a focused subagent:
User Request: "Analyze authentication, payments, and notifications"
│
▼
┌────────────────────────┐
│ Primary Agent │
│ (orchestrates work) │
└────────────────────────┘
│ │ │
┌────────┘ │ └────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Subagent │ │ Subagent │ │ Subagent │
│ Auth │ │ Payments │ │ Notifs │
└──────────┘ └──────────┘ └──────────┘
│ │ │
└──────────────┼──────────────┘
▼
┌────────────────────────┐
│ Combined Response │
└────────────────────────┘
const agent = new ProbeAgent({
path: './src',
enableDelegate: true,
// Optional: configure limits
// Uses environment variables by default
});Environment Variables:
| Variable | Default | Description |
|---|---|---|
MAX_CONCURRENT_DELEGATIONS |
3 | Max simultaneous subagents |
MAX_DELEGATIONS_PER_SESSION |
10 | Max subagents per session |
DELEGATION_QUEUE_TIMEOUT |
60000 | Queue wait timeout (ms). Set to 0 to disable. |
DELEGATION_TIMEOUT |
300 | Subagent timeout (seconds) |
DELEGATION_TIMEOUT_SECONDS |
300 | Alternative timeout (seconds, higher priority) |
DELEGATION_TIMEOUT_MS |
300000 | Timeout in milliseconds (highest priority) |
The AI uses the delegate tool to create subagents:
<delegate>
<task>Analyze authentication module for security vulnerabilities</task>
</delegate>| Parameter | Type | Required | Description |
|---|---|---|---|
task |
string | Yes | Complete, self-contained task description |
Subagents automatically inherit from the parent:
- Workspace path and allowed folders
- AI provider and model
- Bash configuration (if enabled)
- MCP configuration (if enabled)
Subagents are created with focused settings:
promptType: 'code-researcher'(always)enableDelegate: false(prevents recursion)- Limited iterations (respects parent's budget)
- Disabled mermaid/JSON validation (faster)
// Defaults (configurable via environment)
maxConcurrent: 3 // Global limit
maxPerSession: 10 // Per-session limit
queueTimeout: 60000 // Queue wait timeout (ms)When limits are reached, delegations queue:
- Try immediate acquisition
- If global limit reached, enter FIFO queue
- Wait up to
queueTimeoutms - Process when slot becomes available
- Timeout error if wait exceeds limit
import { DelegationManager } from '@probelabs/probe/agent';
const manager = new DelegationManager();
manager.maxConcurrent = 5;
manager.maxPerSession = 20;
const agent = new ProbeAgent({
path: './src',
enableDelegate: true,
delegationManager: manager
});The AI automatically determines when to delegate:
const agent = new ProbeAgent({
path: './src',
enableDelegate: true
});
// Complex request - AI may delegate
await agent.answer(`
I need a comprehensive analysis:
1. Security review of authentication
2. Performance audit of database queries
3. Code quality assessment of API handlers
`);Use the delegate function directly:
import { delegate } from '@probelabs/probe/agent';
const result = await delegate({
task: 'Find all SQL injection vulnerabilities',
timeout: 300,
path: '/path/to/project',
provider: 'anthropic',
debug: true
});
console.log(result);Request structured output from subagents:
const result = await delegate({
task: 'List all API endpoints',
schema: {
type: 'object',
properties: {
endpoints: {
type: 'array',
items: {
type: 'object',
properties: {
method: { type: 'string' },
path: { type: 'string' },
handler: { type: 'string' }
}
}
}
}
}
});
const data = JSON.parse(result);
console.log(data.endpoints);delegate({
// Required
task: string, // Task description
// Timeout
timeout: number, // Seconds (default: 300)
// Context inheritance
path: string, // Workspace path
allowedFolders: string[], // Allowed folders
provider: string, // AI provider
model: string, // AI model
// Capabilities
enableBash: boolean, // Enable bash tool
bashConfig: object, // Bash configuration
enableMcp: boolean, // Enable MCP
mcpConfig: object, // MCP configuration
// Advanced
currentIteration: number, // Parent's iteration count
maxIterations: number, // Max iterations allowed
schema: object, // Response JSON schema
parentSessionId: string, // Parent session ID
delegationManager: object, // Custom manager
// Debug
debug: boolean, // Debug logging
tracer: object // Telemetry tracer
})Subagents cannot delegate further:
// In subagent creation:
new ProbeAgent({
enableDelegate: false // Always false for subagents
});This prevents:
- Infinite delegation chains
- Runaway resource usage
- Stack overflow errors
| Error | Cause | Solution |
|---|---|---|
Task parameter is required |
Empty task | Provide non-empty task |
Maximum delegations per session reached |
Session limit exceeded | Wait or increase limit |
Delegation queue timeout |
Queue wait exceeded | Increase timeout or reduce load |
Delegation timed out |
Subagent took too long | Increase timeout |
Delegate agent returned empty response |
Subagent failed | Check task clarity |
Delegation always cleans up:
- Slots released even on error
- Timeouts cleared
- Counters decremented
try {
const result = await delegate({ task: '...' });
} catch (error) {
// Slot already released
console.error('Delegation failed:', error.message);
}import { getDelegationStats } from '@probelabs/probe/agent';
const stats = getDelegationStats();
console.log({
active: stats.globalActive,
maxConcurrent: stats.maxConcurrent,
maxPerSession: stats.maxPerSession,
queueSize: stats.queueSize,
sessions: stats.sessionCount
});If tracer is provided:
// Events recorded:
'delegation.started'
'delegation.completed'
'delegation.failed'
// Attributes:
'delegation.session_id'
'delegation.parent_session_id'
'delegation.duration_ms'
'delegation.response_length'
'delegation.success'// Good: specific, self-contained
await delegate({
task: 'Analyze the authentication module in src/auth/ for security vulnerabilities. Check for: hardcoded credentials, SQL injection, XSS, and improper session handling.'
});
// Bad: vague, dependent on context
await delegate({
task: 'Check security'
});// Quick analysis
await delegate({
task: 'Count functions in utils.ts',
timeout: 60
});
// Deep analysis
await delegate({
task: 'Comprehensive security audit',
timeout: 600
});const stats = getDelegationStats();
if (stats.globalActive >= stats.maxConcurrent - 1) {
console.warn('Near delegation limit');
}- Tools Reference - All available tools
- API Reference - ProbeAgent configuration
- Environment Variables - Configuration