Skip to main content
SelfManagedStateAgent is an event-driven state machine. Instead of a single run function, you implement two callbacks — start and onToolResults — and manage state explicitly with task.save() and task.restore(). It’s harder to implement than auto-managed state agents, but has no runtime constraints. Use it when you need parallel tool calls, custom state persistence, or full control over execution flow.

Lifecycle

  1. start is called once with the agent’s input. Save any state you’ll need later, then return output() to finish or callTools() to request tool execution.
  2. The runtime executes the requested tools.
  3. onToolResults is called with the results. Restore your state, process the results, and return output() to finish or callTools() to continue the loop.

Basic structure

Self-managed state agents do not use the "use agent" directive. That directive is only for auto-managed state agents.
The description field is optional and deprecated as of @guildai/agents-sdk 0.4.0. Guild generates the agent’s published description automatically from its code, so setting description no longer affects the published description.

State persistence

Use task.save() and task.restore() to persist state between tool calls. State must conform to your stateSchema.
task.restore() returns undefined if no state has been saved yet.

Return types

Every callback must return one of two results:

output(value)

Completes the agent and returns the output value.

callTools(calls)

Requests one or more tool calls. The runtime executes them and calls onToolResults with the results.
Returning multiple tool calls executes them in parallel — this is the primary advantage over auto-managed state agents.

ask(prompt)

A shorthand for prompting the user. Wraps callTools with the ui_prompt tool.

Examples

Interactive agent: Marco Polo

A simple game that demonstrates the full save/restore loop with user interaction.

Parallel tool calls: Multi-issue summary

Fetch multiple GitHub issues in parallel and summarize them — something auto-managed state agents can’t do in a single round.

Error handling

Throw an error from start or onToolResults to fail the agent. Tool errors arrive in the results array as objects with an error property.

When to use self-managed state

The runtime only supports @guildai/agents-sdk and zod. You cannot import external npm packages or Node.js built-in modules — agents run in a sandboxed environment.