Workers API
The WorkersApi enables executing worker agents from your server. Workers are task-based agents that run steps sequentially and return an output value.
Basic Usage
WorkersApi Reference
execute()
Execute a worker and stream the response.
Parameters:
| Parameter | Type | Description |
|---|---|---|
agentId | string | The worker agent ID |
input | Record<string, unknown> | Input values for the worker |
options | WorkerExecuteOptions | Optional configuration |
Options:
continue()
Continue execution after client-side tool handling.
Use this when the worker has tools without server-side handlers. The execution pauses with a client-tool-request event, you execute the tools, then call continue() to resume.
Tool Handlers
Provide tool handlers to execute tools server-side:
Tools defined in the worker protocol but not provided as handlers become client tools — the execution pauses and emits a client-tool-request event.
Stream Events
Workers emit standard stream events plus worker-specific events.
Worker Events
Common Events
| Event | Description |
|---|---|
start | Execution started |
finish | Execution completed |
text-start | Text generation started |
text-delta | Text chunk received |
text-end | Text generation ended |
block-start | Step started |
block-end | Step completed |
tool-input-available | Tool arguments ready |
tool-output-available | Tool result ready |
client-tool-request | Client tools need execution |
error | Error occurred |
Extracting Output
To get just the worker's output value:
Client Tool Continuation
When workers have tools without handlers, execution pauses:
The client-tool-request event includes:
Streaming to HTTP Response
Convert worker events to an SSE stream:
Cancellation
Use an abort signal to cancel execution:
Error Handling
Errors can occur at different levels:
Error types include:
| Type | Description |
|---|---|
validation_error | Invalid input |
not_found_error | Worker not found |
provider_error | LLM provider error |
tool_error | Tool execution failed |
execution_error | Worker step failed |
Full Example
Next Steps
- Workers Protocol — Worker protocol reference
- Streaming — Understanding stream events
- Tools — Tool handler patterns