Follow this sequence for reliable delegation:
- Verify bridge readiness —
bridge_healthif uncertain - Discover agents —
agents_discoverto find available agents and their providers - Select provider — if the agent supports multiple providers, choose one based on the
providersfield in the discovery response - Create a session —
sessions_createwithagentIdand optionalmetadata.provider(returnssessionId) - Submit the prompt —
sessions_promptwithsessionId,prompt, and optionalruntimeParams - Check status —
sessions_statusif the task is long-running - Continue session —
sessions_promptagain for follow-up requests (with optionalruntimeParamsoverrides) - Close session —
sessions_closewhen work is complete
For one-shot tasks, use tasks_delegate (with optional runtimeParams) instead of steps 3–8.
Sessions progress through these states:
- active — session created, ready for requests
- busy — session is processing a prompt
- idle — prompt completed, session ready for more requests
- failed — session encountered an unrecoverable error
Note: When a session is closed (via
sessions_closeor automatic expiry), it is deleted from the executor — not transitioned to a "closed" state. You cannot query a closed session viasessions_status; it will return "Session not found".
[create] → active → busy → idle → busy → ...
↓
failed
[close/expiry] → session deleted (not queryable)
Sessions also expire automatically (in-process executor only):
- TTL expiry — session exceeded maximum lifetime (default: 1 hour)
- Idle expiry — session idle beyond threshold (default: 10 minutes)
Expired sessions are reaped and the agent's onSessionClose hook is called with reason 'expired'.
Note: The
WorkerExecutordoes not have automatic session expiry. Worker sessions persist in the local session map until explicitly closed viasessions_close. Worker processes manage their own internal session lifecycle independently.
When using MultiProviderCompanionAgent, sessions can be bound to a specific AI provider at creation time.
Pass metadata.provider to sessions_create to select the AI provider for the session:
sessions_create({
agentId: "multi-provider-agent",
metadata: {
provider: "anthropic",
runtimeParams: { model: "claude-sonnet-4-20250514", temperature: 0.7 }
}
})
→ { sessionId: "abc-123", agentId: "multi-provider-agent", status: "active" }
metadata.provider— selects the AI provider for all prompts in this sessionmetadata.runtimeParams— sets session-level default parameters (model, temperature, etc.)- If
metadata.provideris omitted, the agent uses its configureddefaultProviderId - If
metadata.providerspecifies an unregistered provider,sessions_createfails withBridgeErrorCONFIG
Once a session is created with a specific provider, all prompts in that session use that provider. The provider cannot be changed mid-session — create a new session to switch providers.
RuntimeParams allow dynamic control of AI generation parameters at each request, without restarting the server.
| Parameter | Type | Description |
|---|---|---|
model |
string |
Model identifier (e.g., "gpt-4o", "claude-sonnet-4-20250514") |
temperature |
number (0–2) |
Sampling temperature; higher = more random |
maxTokens |
number (positive int) |
Maximum tokens to generate |
topP |
number (0–1) |
Nucleus sampling probability |
topK |
number (positive int) |
Top-K sampling parameter |
stopSequences |
string[] |
Sequences that stop generation |
systemPrompt |
string |
System prompt override for this request |
providerSpecific |
Record<string, unknown> |
Provider-native parameters not covered by common fields |
ProviderConfig.defaults < session metadata.runtimeParams < prompt-level runtimeParams
Only defined fields override lower-priority values. undefined fields are ignored during merge. providerSpecific is shallow-merged across all layers.
sessions_prompt({
sessionId: "abc-123",
prompt: "Explain quantum computing",
runtimeParams: {
temperature: 0.2,
maxTokens: 500,
systemPrompt: "You are a physics professor. Explain concepts simply."
}
})
→ { text: "Quantum computing uses...", stopReason: "end_turn", usage: { inputTokens: 45, outputTokens: 120 } }
tasks_delegate({
agentId: "multi-provider-agent",
prompt: "Summarize this document",
metadata: { provider: "openai" },
runtimeParams: { temperature: 0, maxTokens: 200 }
})
→ { sessionId: "xyz-789", text: "The document covers...", stopReason: "end_turn", usage: { inputTokens: 80, outputTokens: 50 } }
sessions_createreturns asessionId— use it for all subsequent calls- Pass
sessionIdtosessions_prompt,sessions_status,sessions_close,sessions_cancel - Report
sessionIdin all outputs for traceability
- Don't split related work across sessions
- Only create parallel sessions when the user explicitly requests it
- Reuse sessions to reduce overhead
If a session becomes invalid:
- Report explicitly — tell the user the session is no longer valid
- Explain reason — why the session was invalidated (expired, failed, closed)
- Offer recovery — suggest creating a new session
- Preserve context — maintain conversation context despite session loss
For single tasks that don't need follow-up:
tasks_delegate({
agentId: "my-agent",
prompt: "Analyze this function for bugs"
})
→ { sessionId: "abc-123", text: "Found 2 issues...", stopReason: "end_turn" }
tasks_delegate creates a session, sends the prompt, and closes the session in one call.
For interactive work requiring multiple exchanges:
sessions_create({ agentId: "my-agent" })
→ { sessionId: "abc-123", agentId: "my-agent", status: "active" }
sessions_prompt({ sessionId: "abc-123", prompt: "Analyze this codebase" })
→ { text: "Found 3 issues...", stopReason: "end_turn" }
sessions_prompt({ sessionId: "abc-123", prompt: "Now suggest fixes" })
→ { text: "Here are the fixes...", stopReason: "end_turn" }
sessions_close({ sessionId: "abc-123" })
→ { closed: true, sessionId: "abc-123" }
To cancel an in-flight prompt:
sessions_cancel({ sessionId: "abc-123", requestId: "req-456" })
The agent's cancel hook is called if implemented.
- Don't transform — pass results through unchanged
- Don't summarize — keep full structured data
- Don't flatten — maintain nested structure
- Don't filter — include all fields unless explicitly requested
{
"text": "Agent response text",
"stopReason": "end_turn",
"requestId": "req-456",
"usage": {
"inputTokens": 100,
"outputTokens": 250
}
}The usage field is present when the AI provider reports token consumption. It contains inputTokens (tokens consumed by the prompt) and outputTokens (tokens generated in the response). Not all providers or configurations guarantee usage data.
{
"sessionId": "abc-123",
"agentId": "my-agent",
"status": "idle",
"createdAt": 1719000000000,
"lastActivityAt": 1719000060000
}- Task is complete and no follow-up is expected
- User explicitly requests to stop
- Session has been idle too long (automatic via idle expiry)
- Unrecoverable error occurred
- TTL expiry — sessions expire after configured TTL (default: 1 hour)
- Idle timeout — sessions close after idle threshold (default: 10 minutes)
- Max sessions — new sessions are rejected when capacity is reached
- The
InProcessExecutorruns a periodic reaper that cleans up expired sessions
Worker sessions are not automatically reaped. They persist until explicitly closed or the server shuts down.