Skip to main content

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

generate()

Execute a worker and return the output directly.

typescript

Runs the worker to completion and returns the output value. This is the simplest way to execute a worker.

Returns:

typescript

Throws: WorkerError if the worker fails or completes without producing output.

execute()

Execute a worker and stream the response. Use this when you need to observe intermediate events like text deltas, tool calls, or progress tracking.

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.

Shared Options

All methods accept the same options:

typescript

Parameters:

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

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.

Error Handling

WorkerError (generate)

generate() throws a WorkerError on failure. The error includes an optional sessionId for constructing debug URLs:

typescript

Stream Errors (execute)

When using execute(), errors appear as stream events:

typescript

Error Types

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

Cancellation

Use an abort signal to cancel execution:

typescript

With execute() and a manual controller:

typescript

Streaming

When you need real-time visibility into the worker's execution — text generation, tool calls, or progress — use execute() instead of generate().

Basic Streaming

typescript

Streaming to HTTP Response

Convert worker events to an SSE stream:

typescript

Client Tool Continuation

When workers have tools without handlers, execution pauses:

typescript

The client-tool-request event includes:

typescript

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

Full Examples

generate()

typescript

execute()

For full control over streaming events and progress tracking:

typescript

Next Steps