AutomaticallyManagedStateAgent
The recommended pattern for most coded agents. You implement anasync run function, and the runtime handles state management.
Example
Add the"use agent" directive at the top of your file so the runtime can manage state between tool calls.
Input and output schemas
Define your schemas using Zod. The runtime uses them to validate input and expose the agent as a typed tool for orchestrating agents.Error handling
Any exception thrown from yourrun function is returned to the calling agent or user. Use standard TypeScript error handling:
Self-managed state
SelfManagedStateAgent is an event-driven state machine for when AutomaticallyManagedStateAgent doesn’t fit — primarily parallel tool calls and custom state persistence. It’s harder to implement but has no runtime constraints.
Use it when:
- You need to make parallel tool calls
AutomaticallyManagedStateAgentdoesn’t fit your execution model- You require fine-grained control over state persistence
Documentation for
SelfManagedStateAgent is coming soon. The key pattern is implementing start and onToolResults callbacks instead of a single run function, with task.save() and task.restore() for state persistence between tool calls.When to use each type
| Situation | Recommended type |
|---|---|
| Task can be described as a prompt + tools | llmAgent |
| Algorithmic, deterministic logic | AutomaticallyManagedStateAgent |
| Need parallel tool calls or complex state | SelfManagedStateAgent |
| Want to minimize LLM costs | AutomaticallyManagedStateAgent or SelfManagedStateAgent |
Performance tips
- LLM calls are expensive. Store the result of
task.llm.generateText()in a variable if you need it more than once. - Environment setup is slow. Reuse Docker environments within a run rather than creating and destroying them repeatedly.
- Batch operations. Group related API calls to reduce round-trips.
- Use progress logs. Keep users informed during long-running operations with
task.ui?.notify(progressLogNotifyEvent(...)).