Skip to content

Built-in Functions

This page lists every user-facing built-in function and built-in language construct in Agency. These are available in every .agency file without an import statement.

For the auto-imported standard library (print, sleep, read, fetch, range, ...) see Agency's Standard Library and the stdlib reference.

LLM

NameSignatureDescription
llmllm(prompt: any, options?): TSend a message to an LLM and return its response. The prompt is a string, or an array mixing text and image()/file() attachments. The return type T is inferred from the variable annotation at the call site and compiled to a JSON schema. options accepts model, provider, apiKey, maxTokens, temperature, stream, reasoningEffort ("low"/"medium"/"high"), thinking ({ enabled, budgetTokens? }), tools, hostedTools, memory, maxToolResultChars, retries, timeout, backoff ({ initial?, factor?, max? }), and metadata. The same fields can also be passed as named arguments. See LLMs.

Checkpointing

NameSignatureDescription
checkpointcheckpoint(): numberTake a snapshot of the current execution state and return a checkpoint ID.
getCheckpointgetCheckpoint(id: number): anyReturn the full checkpoint object for a given ID.
restorerestore(checkpoint, options?): voidRestore execution to a previously captured checkpoint. Accepts either a checkpoint object or ID, plus an options object (e.g. { maxRestores: 3 } or variable overrides). See Checkpointing.

Interrupts and Handlers

NameSignatureDescription
interruptinterrupt <kind>(message, payload?)Statement (not a regular function call) that throws an interrupt of the given kind. Execution state is captured so that, once responded to, execution resumes from exactly this point. See Interrupts.
approveapprove(value?): anyUsed inside a handle ... with block to approve the wrapped action, optionally substituting a return value.
rejectreject(value?): anyUsed inside a handle ... with block to block the wrapped action.
propagatepropagate(): anyUsed inside a handle ... with block to pass the interrupt up to the next handler in the chain. See Handlers.

Concurrency

NameSignatureDescription
forkfork(items) as <name> { ... }Run multiple branches in parallel and wait for all of them to finish. Returns an array of results, one per item. Pass shared: true to share globals across branches (they're isolated by default).
racerace(items) as <name> { ... }Like fork, but returns as soon as the first branch completes and cancels the rest. Also accepts shared: true. See Concurrency.

Callbacks

NameSignatureDescription
callbackcallback(eventName) as <data> { ... }Register a callback for an event. The block receives the event's data as an argument. See Callbacks.

Result Type

NameSignatureDescription
successsuccess(value): ResultWrap a value in a successful Result.
failurefailure(error, value?): ResultWrap an error (and optionally a value) in a failed Result.
isSuccessisSuccess(result): booleanCheck whether a Result is a success.
isFailureisFailure(result): booleanCheck whether a Result is a failure. See Error Handling.

Function and Tool Methods

These are built-in methods you can call on any Agency function or tool value (they each return a new tool, so they chain).

NameSignatureDescription
.partialfn.partial(name: value, ...)Bind some of a function's arguments by name, producing a new function that takes the rest. See Partial Application.
.describefn.describe(text): toolOverride the tool description an LLM sees for this function.
.renamefn.rename(name): toolGive this tool a distinct name (the name the LLM sees). Use when deriving several tools from one function, since .partial()/.describe()/import aliases keep the base name and would collide in one llm({ tools }) call.
.preapprovefn.preapprove(): toolAuto-approve every interrupt this function raises.

Types and Schemas

NameSignatureDescription
schemaschema<T>Expression that returns the Zod schema for a type T. Useful when passing a schema to a TypeScript function or validator. See Schemas.

Built-in Variables

NameTypeDescription
__dirnamestringThe absolute path of the directory containing the current .agency file.
colorobjectTerminal color helpers for print output, e.g. color.red("error"), color.dim("note").

Debugging

NameSignatureDescription
debuggerdebugger "<label>"?No-op when running the agent normally, but pauses execution when running under the Agency debugger. Accepts an optional label string. See Debugger.

MCP

NameSignatureDescription
registerToolsregisterTools(tools)Register MCP tools with the agent. See MCP.

Partial Results

NameSignatureDescription
saveDraftsaveDraft(value)Save a partial result to be returned if the function is interrupted. See Partial Results.