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
startis called once with the agent’s input. Save any state you’ll need later, then returnoutput()to finish orcallTools()to request tool execution.- The runtime executes the requested tools.
onToolResultsis called with the results. Restore your state, process the results, and returnoutput()to finish orcallTools()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
Usetask.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.
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 fromstart or onToolResults to fail the agent. Tool errors arrive in the results array as objects with an error property.