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

typescript

WorkersApi Reference

execute()

Execute a worker and stream the response.

typescript

Parameters:

ParameterTypeDescription
agentIdstringThe worker agent ID
inputRecord<string, unknown>Input values for the worker
optionsWorkerExecuteOptionsOptional configuration

Options:

typescript

continue()

Continue execution after client-side tool handling.

typescript

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:

typescript

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

typescript

Common Events

EventDescription
startExecution started
finishExecution completed
text-startText generation started
text-deltaText chunk received
text-endText generation ended
block-startStep started
block-endStep completed
tool-input-availableTool arguments ready
tool-output-availableTool result ready
client-tool-requestClient tools need execution
errorError occurred

Extracting Output

To get just the worker's output value:

typescript

Client Tool Continuation

When workers have tools without handlers, execution pauses:

typescript

The client-tool-request event includes:

typescript

Streaming to HTTP Response

Convert worker events to an SSE stream:

typescript

Cancellation

Use an abort signal to cancel execution:

typescript

Error Handling

Errors can occur at different levels:

typescript

Error types include:

TypeDescription
validation_errorInvalid input
not_found_errorWorker not found
provider_errorLLM provider error
tool_errorTool execution failed
execution_errorWorker step failed

Full Example

typescript

Next Steps