Skip to content
providerkit
GitHub

AgentHost

Defined in: agents/src/types.ts:669

Host callbacks are trusted code, never functions reconstructed from a client snapshot. Data passed to callbacks is a frozen copy, not the loop’s live state. Without checkpoint, execution is in-memory only. Checkpoints are not leases or exactly-once effect guarantees.

optional answerNow?: (request, options) =>
| Decision
| Promise<Decision | undefined>
| undefined;

Defined in: agents/src/types.ts:711

Optional standing host policy, or an in-process wait for a person under the run signal. Returning undefined leaves the request pending so the run suspends. While this callback waits, limits.activeMs is paused; limits.elapsedMs, treeLimits.elapsedMs and the caller’s signal still apply.

Parameter Type
request PendingRequest
options { signal: AbortSignal; }
options.signal AbortSignal

| Decision | Promise<Decision | undefined> | undefined


optional authorizeResume?: (snapshot) => void | Promise<void>;

Defined in: agents/src/types.ts:684

Load/compare authoritative state and check ownership and freshness, or throw. The host must also exclude concurrent attempts; this loop does not take a lease.

Parameter Type
snapshot TreeNode

void | Promise<void>


optional beforeStep?: (step, decided?) => void | Promise<void>;

Defined in: agents/src/types.ts:735

Required before every workflow effect or use of an answer. Check current ownership and lease.

Parameter Type
step StepRequest
decided? { decision: Decision; request: PendingRequest; }
decided.decision? Decision
decided.request? PendingRequest

void | Promise<void>


optional beforeTool?: (call, decided?) => void | Promise<void>;

Defined in: agents/src/types.ts:720

Recheck current access and resource version, with the arguments that will run. Required for every invocation carrying a decision, and before exposing a client-tool request.

Parameter Type
call ToolRequest
decided? { decision: Decision; request: PendingRequest; }
decided.decision? Decision
decided.request? PendingRequest

void | Promise<void>


optional canRunConcurrently?: (request) => boolean;

Defined in: agents/src/types.ts:748

Both this callback and the tool’s isConcurrencySafe flag must allow batching.

Parameter Type
request ToolRequest

boolean


optional checkpoint?: (snapshot) => void | Promise<void>;

Defined in: agents/src/types.ts:681

Persist the whole snapshot before returning. Writes are serialized within this attempt.

Parameter Type
snapshot TreeNode

void | Promise<void>


optional child?: (request, execute) => Promise<ChildEnvelope>;

Defined in: agents/src/types.ts:753

Optional in-process child binding under this root’s lease and cancellation. A host may return a saved envelope. To resume a suspended SDK child it must call execute in a new/resumable segment, not treat a cached suspension as completion. execute may be called once, and must settle before the hook returns.

Parameter Type
request ChildRequest
execute () => Promise<ChildEnvelope>

Promise<ChildEnvelope>


optional inbox?: InputInbox;

Defined in: agents/src/types.ts:672

New user messages for the root’s active conversation owner, never every specialist. A durable host must supply durable authenticated storage. Does not answer human requests.


optional loadMedia?: (ref, run) => string | Promise<string>;

Defined in: agents/src/types.ts:756

Check that this run’s owner may read the media, then return its bytes as base64. Called once per id per attempt, before the model call that needs it. An id is not permission.

Parameter Type
ref MediaRef
run { runId: string; signal: AbortSignal; }
run.runId string
run.signal AbortSignal

string | Promise<string>


optional needsApproval?: (call) => boolean | Promise<boolean>;

Defined in: agents/src/types.ts:686

May require approval for an unmarked tool, never waive a tool’s needsApproval.

Parameter Type
call ToolRequest

boolean | Promise<boolean>


optional onContextLimit?: (limit) => void;

Defined in: agents/src/types.ts:768

Reports a length rejection from a turn. Counts are present only if the provider supplied them; the configured window and earlier calls are not measurements of this request.

Parameter Type
limit { contextLimit?: number; inputTokens?: number; model: string; operationId: string; provider: string; runId: string; }
limit.contextLimit? number
limit.inputTokens? number
limit.model string
limit.operationId string
limit.provider string
limit.runId string

void


optional prepareRequest?: (draft) =>
| {
ask?: ApprovalAsk;
scope: RequestScope;
}
| Promise<{
ask?: ApprovalAsk;
scope: RequestScope;
}>;

Defined in: agents/src/types.ts:702

Required to open a request. Supplies trusted actor, resource version and expiry. Only an approval’s presentation/edit policy may be overridden.

Parameter Type
draft RequestOrigin & Omit<PendingRequest, "step" | "call" | "scope">

| { ask?: ApprovalAsk; scope: RequestScope; } | Promise<{ ask?: ApprovalAsk; scope: RequestScope; }>


optional priceModelCall?: (call) => number;

Defined in: agents/src/types.ts:765

USD for one model call’s usage, from the host’s own rates: no price table ships here, and core’s costUsd(usage, rate) does the arithmetic. Required by limits.costUsd. Called when usage arrives and when the call completes, then saved in the journal, so a resumed run never prices a saved call again, even when the host’s prices change by time of day. Synchronous; return a finite amount of 0 or more.

Parameter Type
call MeasuredModelCall

number


optional progress?: object;

Defined in: agents/src/types.ts:676

Required progress storage, separate from best-effort onEvent. push synchronously accepts a frozen event into a bounded buffer; flush must report any write failure, not hide it. The loop awaits flush before every checkpoint and final outcome, including children.

flush(): Promise<void>;

Promise<void>

push(event): void;
Parameter Type
event AgentEvent

void


optional reconcileStep?: (step, attempt, signal) =>
| {
outcome: "not_run";
receipt: string;
}
| {
outcome: "unknown";
}
| Promise<
| {
outcome: "not_run";
receipt: string;
}
| {
outcome: "unknown";
}>;

Defined in: agents/src/types.ts:739

Parameter Type
step StepRequest
attempt { attemptId: string; error?: StepError; }
attempt.attemptId string
attempt.error? StepError
signal AbortSignal

| { outcome: "not_run"; receipt: string; } | { outcome: "unknown"; } | Promise< | { outcome: "not_run"; receipt: string; } | { outcome: "unknown"; }>


optional reconcileToolRetry?: (attempt, context) =>
| {
outcome: "not_run";
receipt: string;
}
| {
outcome: "unknown";
}
| Promise<
| {
outcome: "not_run";
receipt: string;
}
| {
outcome: "unknown";
}>;

Defined in: agents/src/types.ts:727

Authoritatively query the external service for this exact failed attempt. Return not_run only when it cannot still complete. An error string or literal key is not proof. Never dispatch from this callback. Requests and control tools cannot use this retry path.

Parameter Type Description
attempt { request: ToolRequest; result: { content: string; kind: | "denied" | "cancelled" | "interrupted" | ToolFailure | "refused" | "unknown_tool" | "disabled_tool" | "invalid_output"; ok: false; }; } -
attempt.request ToolRequest -
attempt.result { content: string; kind: | "denied" | "cancelled" | "interrupted" | ToolFailure | "refused" | "unknown_tool" | "disabled_tool" | "invalid_output"; ok: false; } -
attempt.result.content string -
attempt.result.kind | "denied" | "cancelled" | "interrupted" | ToolFailure | "refused" | "unknown_tool" | "disabled_tool" | "invalid_output" refused: host.screenCall refused the call before it started.
attempt.result.ok false -
context LifecycleContext -

| { outcome: "not_run"; receipt: string; } | { outcome: "unknown"; } | Promise< | { outcome: "not_run"; receipt: string; } | { outcome: "unknown"; }>


optional resolveRequest?: (request, evidence) =>
| Resolution
| Promise<Resolution>;

Defined in: agents/src/types.ts:717

Authenticate evidence, validate form data and consume it for this exact request. Recovery of the same host claim must return the same decision; another claim must be refused.

Parameter Type
request PendingRequest
evidence unknown

| Resolution | Promise<Resolution>


optional screenCall?: (call, run) =>
| CallVerdict
| Promise<CallVerdict | undefined>
| undefined;

Defined in: agents/src/types.ts:696

Runs for each call the model or a code program makes, right before it would start. Return nothing to let it run. { refuse } answers this call with that text instead, as the model’s result for it: the tool does not run and no request opens. { cancel } answers this call and every call after it in its turn with that text, and holds after a restart. A call that needs approval is also screened before its approval opens, so nobody is asked about a call the host refuses, and again before it runs, since the calls ahead of it ran in the meantime. Specialists’ calls come here too, each with its own run’s earlier. Decide from earlier rather than process state: after a restart, calls that had not started are screened again.

Parameter Type
call ToolRequest
run ScreenContext

| CallVerdict | Promise<CallVerdict | undefined> | undefined