Use @phaseo/agent-sdk when your application needs more than one-shot text generation:
- multi-step tool loops
- local runtime tools
- resumable runs from SDK-returned state
- explicit human approval pauses
- typed final outputs
- gateway-backed model turns through the existing TypeScript SDK
The package is an installable SDK, not a hosted agent platform. You bring the application, deployment model, and any persistence strategy you want around returned run state.
State model
The Agent SDK does not persist runs into any Phaseo-hosted service.
run() returns the full state needed to continue later.
- If your application wants resumability across requests or process restarts, persist that returned state in your own application store.
continueRun() accepts that prior run state directly.
Nothing is persisted by Phaseo outside your application.
Install
What the SDK ships
createAgent()
defineTool()
createGatewayAgentClient()
continueRun() for continuing from previously returned run state
stream() and continueStream() for incremental, replayable results
- stop-condition helpers such as
stepCountIs(), maxCost(), and hasToolCall()
First agent
Mental model
The runtime loop does four things:
- sends the current message state to the model client
- executes any returned local tool calls
- appends tool results into the next turn
- returns the updated run state after each completed step boundary
That gives your application a resumable loop without forcing you into a hosted orchestration product.
Core primitives
createAgent()
Use createAgent() to define:
- one stable
id
- instructions
- one model or preset
- one small tool list
- optional output parsing
- optional human review rules
- optional retry and tool-execution controls
Keep the first agent narrow. One workflow and one or two tools is usually enough.
Define local runtime tools with:
id
description
- optional JSON
parameters
- optional
timeoutMs
inputSchema and outputSchema runtime validators
execute(), execute: false, or human-in-the-loop callbacks
requireApproval, onError, nextTurnParams, and progress events
If a timeout fires, the runtime aborts context.signal, marks the run as failed, and rethrows the timeout error.
Schemas can be a function or any object exposing parse() or safeParse(). Invalid model arguments and invalid tool results fail before they cross the tool boundary.
Gate a side-effecting tool per call:
The run pauses with run.pause.pendingToolCalls. Resume by exact call ID so concurrent calls cannot be confused:
Set execute: false for work performed by your application and provide its result through toolOutputs. For an interactive tool, return null from onToolCalled; after continuation, onResponseReceived can validate or transform the supplied human response.
An async generator can publish preliminary results and return one final result:
Progress appears as tool.preliminary_result events and in the step’s preliminaryResults.
Streaming results
stream() starts the same state machine with a streaming model client. Its consumers are replayable, so UI, telemetry, and persistence code can read concurrently:
Use getReasoningStream(), getItemsStream(), getToolStream(), or getFullStream() for more specific consumers. cancel() aborts the run.
Render typed run items
getItemsStream() yields AgentItem<TOutput>, a discriminated union that is safe to switch on:
The same ordered item contract is available on completed.items after either run() or stream(). Provider output is normalized into message, reasoning, tool-call, tool-result, error, and final-output items. Provider-specific fields remain available through rawProviderItem on normalized provider items.
Stop conditions and dynamic turns
Stop conditions compose as an array; the first matching condition records its reason and returns a stopped run:
Tools can set application context with context.setContext() and override the immediately following turn with nextTurnParams.
createGatewayAgentClient()
Use the gateway-backed adapter when model turns should execute through Phaseo Gateway.
It can carry gateway-native controls such as:
responseFormat
plugins
gatewayTools
toolChoice
webSearchOptions
providerOptions
promptCacheKey
includeMeta
That lets your app keep routing, search, structured outputs, and plugin defaults close to the model client instead of rebuilding raw request payloads on every run.
Application-owned persistence
If your application needs resumability, persist the returned AgentRunResult directly or provide a state accessor with asynchronous load(runId) and save(result) methods. A continued run can then use runId without carrying the serialized record through every layer.
The SDK intentionally does not ship persistence adapters or a hosted state backend.
That means you can:
- keep one-shot runs entirely in-process
- serialize paused or incomplete runs into your own application records
- reload that saved run state and pass it back to
continueRun() later
Human review and continuation
Use humanReview when a run should checkpoint and wait for approval:
Continue with explicit human input:
Typed outputs
Use parseOutput when your app wants a typed final value:
For stricter model behavior, combine that with structured outputs on the gateway adapter:
Runtime controls
Model retries
Use modelRetry when transient model failures should retry before the run is persisted as failed:
maxRetries counts extra attempts after the first model request.
The persisted step record stores the final retry count as modelAttempts.
If one model turn can safely call several independent tools, set toolExecution.toolConcurrency:
The runtime still preserves tool-result message order.
Preset-driven routing
Use preset when routing, prompt, or parameter defaults should stay managed in the dashboard instead of being hard-coded in app code:
Event hooks
Use onEvent when your application wants lifecycle hooks for logs, telemetry, or internal workflows.
Current events include:
run.started
run.resumed
step.started
step.completed
step.failed
step.cancelled
model.requested
model.completed
model.failed
tool.started
tool.completed
tool.failed
checkpoint.saved
run.waiting_for_human
run.cancelled
run.completed
run.failed
If one step succeeds, the runtime emits step.completed after the checkpointed step has been persisted.
Error handling
Gateway failures are rethrown as AgentGatewayError:
If the failure came from the gateway, failed runs and steps also persist errorDetails.
Included examples
The package currently ships these examples:
examples/research-brief-agent.ts
examples/support-triage-agent.ts
examples/coding-review-agent.ts
examples/parallel-tool-agent.ts
Current scope
The SDK is intentionally focused on application-building primitives:
- local or app-owned checkpoint persistence
- gateway-backed model turns
- local tools
- resumable agent loops
- per-step normalized token usage, cost, warnings, finish reasons, and tool results
It does not try to be a hosted orchestration platform or ship one opinionated remote persistence backend.
Last modified on July 26, 2026