Skip to main content
An agent definition combines a model, instructions, tools, and limits. You define it once and run it from any handler.

Define an agent

Create the agent at module scope with agent():
The main options are:

Run an agent

Run the agent with yield* agent.run(input):
Without the conversation or messages option, each call to run starts a run without history. To keep history between requests, see Choose a service or a Virtual Object.

Choose a service or a Virtual Object

Run the agent in a service when each request is a self-contained task. Run it in a Virtual Object with a conversation when a request continues an earlier one, such as the next turn of a chat.
  • Service: each request is a self-contained task. The handler calls agent.run(input) without a conversation, so each request starts a new conversation, and the caller doesn’t need a key. Examples: classify a ticket, write a research report, and run a coding task in a sandbox.
  • Virtual Object: requests to the same key are follow-ups. The handler passes conversation: new Conversation() to run. The object key identifies the session, customer, or thread. Restate runs one exclusive handler per key at a time, so each conversation has one writer. Examples: chat, support session, and the quickstart. See Sessions.
A conversation can outlive the resources that its runs used. If the agent creates a resource per request, such as the sandbox in the coding task example, the history of a later request refers to a resource that no longer exists. Run such an agent in a service, or keep the resource per key. See Sandboxes. A Virtual Object handler that runs the same agent several times in one request can also use a conversation, so that each run has the earlier runs in its history. See Review loop.

Run results

run returns a RunResult with one of three statuses: If the run fails, for example because a model call fails after its retries or a reply is cut off at the output token limit, run throws instead of returning a result. See Error handling. Read output as the answer only when the status is completed. For a stopped or interrupted run, Restate Harness makes one more model call, without tools, that summarizes the run, and puts the summary in output. If that call fails, output holds a note that no summary could be generated. Set finalize to the model for that call, or to false to skip it. It defaults to the agent’s model. Every result also includes the number of steps, the run’s messages, and its token usage. messages holds the earlier messages, from messages or the loaded conversation, followed by the messages of this run. A conversation with a stored summary loads only the messages after the summary, so messages starts there.

Typed output

Give the agent a Zod output schema. Restate Harness asks the model for output that matches the schema, validates the response, and asks again if the response doesn’t match. Each new attempt counts toward maxSteps:
result.output has the type that the schema defines. The schema must convert to JSON Schema, so Zod transforms aren’t supported. For OpenAI models, see structured output with OpenAI. When the output feeds the next step of your code, throw a TerminalError for a stopped or interrupted run, as in the example.

Run context

Declare a context type on the agent and pass the context to run. Restate Harness passes the context to the instructions function, to every tool, and to every guardrail. It doesn’t add the context to model requests: only what your instructions and tools put into the prompt reaches the model:
When the agent declares a context type, run requires the context option. Use the context for trusted data, such as user IDs, prices, and limits, instead of letting the model supply these values as tool arguments.

Run names

If a handler starts more than one run of the same agent, or of agents without a name, for example in a loop or a race, give each run a distinct name. Without one, a run uses the agent’s name, or agent if the agent has none:
Restate Harness includes the name in the run’s approval signal names, so an approval for one run can’t resolve a tool call in another. See Agents in workflows for a complete example.

Limits

Reaching maxSteps or maxToolCalls stops the run with a result, not an error. Retries are configured separately on the model and on each tool.