Define an agent
Create the agent at module scope withagent():
Run an agent
Run the agent withyield* agent.run(input):
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()torun. 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.
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 Zodoutput 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 torun.
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:
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 aname, 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:
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.