statelyai/agent
An XState-based TypeScript SDK for constraining and replaying LLM workflows
Automated SPDX check for reference only — not legal advice. Disclaimer
GitHub Star History
Full-history star trajectory from GitHub official history. Switch ranges or hover any point for exact counts.
Evidence-Backed Technical Brief
AI Deep ReviewArchitecture & Core Mechanics
Stately Agent is a TypeScript SDK built on XState. setupAgent defines the machine and schemas for input, context, output, and events. During a run, runAgent routes typed requests to host executors such as generateText, streamText, and decide, then feeds the result back as an event or output. For a decision, the model proposes an event from the supplied set; the machine owns guards and transitions. Given the same inputs and recorded results, the control flow can be replayed without assuming the model itself is deterministic.
The executor boundary keeps provider calls out of the statechart. The host chooses models, supplies credentials, runs effects, and stores native XState snapshots. It can use the Vercel AI SDK, raw OpenAI SDK, or TanStack AI adapter, or supply plain functions for a custom integration or a model-free test. Approval states, retries, parallel work, and child agents remain explicit parts of the machine.
Key Capabilities & Differentiators
canReach checks whether a modeled violation state is reachable without calling a model. In the refund example, the model may propose AUTO_REFUND, but the guard permits the transition only at $100 or less. A result of reachable: false is evidence of non-reachability within the modeled scope only when exploration also reports complete: true. For runtime debugging, replay reconstructs a run from its event log without issuing fresh model calls; resuming live work instead uses a stored snapshot and an incoming event.
The brownfield tools address different checks: checkConformance checks a recorded run against a model of existing code, checkParity compares the machine's behavior against recorded effects from the old implementation, and compareRuns classifies outcome differences as same, intended, or regression. wrapWorkflow can capture an inspection and trace baseline before a rewrite, but wrapping an opaque async function does not make its internal rules verifiable. Those rules need to be modeled before reachability checks can say anything about them.
Trade-offs & Limitations
Compared with a hand-written loop around an AI SDK, Stately Agent makes branching and business rules explicit and gives teams tools to verify and replay that control flow. The cost is designing states, event schemas, and guards instead of adding another conditional. For teams already using a graph framework such as LangGraph, the deciding factor is whether XState's reachability checks, recorded-run comparisons, and snapshot restoration solve a concrete problem worth migrating for. A simple prompt-to-response endpoint is unlikely to justify the extra structure.
The guarantee stops at the modeled boundary. A legal event can still be the wrong decision, and a statechart cannot verify a constraint that an executor enforces privately. The host must also provide durable storage, decide how to handle retries, and make external effects safe to repeat; restoring a snapshot is not an exactly-once guarantee. MIT allows commercial embedding, but it does not govern provider terms or the hosted inspector relay.
Hardware & Deployment Guide
Embed @statelyai/agent in a Node.js 22+ application. For TypeScript authoring, use TypeScript 5.4+ and a Standard Schema implementation such as Zod 4, Zod 3.24+, Valibot, or ArkType. The package is ESM-first and also publishes CommonJS builds. Match XState to the exact peer required by the chosen package version: 2.0.0-alpha.41 pairs with 6.0.0-beta.2, while the supplied source checkout requires 6.0.0-beta.4. Core verification and stubbed runs work offline; live provider calls need network access and credentials. Deploy it as an application dependency, not as a turnkey API service.
Validate optional adapter versions separately: the AI SDK adapter needs ai 7.0.128+, raw OpenAI needs openai 5+, and TanStack AI needs @tanstack/ai 1.7+. A mismatched optional SDK can install successfully and fail at import or invocation. The 2.0 line is still alpha: alpha.41 added checkConformance, and alpha.42 changed the AI SDK adapter to default to maxRetries: 0 so run usage can count each provider request. Pin versions and retest retry behavior and persisted snapshots when upgrading.
agent.decide receives allowedEvents; XState guards still decide whether a proposed transition can fire.canReach can test whether a sentinel state is reachable without a model API key; check complete before treating non-reachability as proof.replay reconstructs state from recorded events without another model call.wrapWorkflow captures a baseline, while checkParity compares behavior as phases move into the machine.runAgent inspect option connects to the Stately inspector via @statelyai/sdk and a hosted relay.TypeScript teams building approval-gated support or operations workflows that need explicit business rules, resumable pauses, and auditable replay, or migrating an existing agent in phases with parity checks.
A single model call or a small tool loop is enough, you need a bundled service with managed persistence, or you cannot pin an alpha SDK to its exact XState beta peer.
Quick Start & Installation
pnpm add @statelyai/agent@2.0.0-alpha.41 xstate@6.0.0-beta.2 zodComparison vs. Top Agents & Frameworks Alternatives
Full comparison →| Repository | Stars | 7d Trend | Stack | License | Core Positioning |
|---|---|---|---|---|---|
| agent ★ | 511 | +3 | TypeScript | MIT | An XState-based TypeScript SDK for constraining and replaying LLM workflows |
| ruflo | 82.8k | +342 | TypeScript | MIT | A TypeScript orchestration layer for coding agents, with shared memory and MCP |
| browser-use | 122.7k | +477 | Python | MIT | Python REPL and CDP for adaptive browser automation |
| langgraph | 45.8k | +293 | Python | MIT | Python graph orchestration for stateful, resumable agents and workflows |
AI Context Briefing (for Claude / Cursor / Windsurf)
Standardized architecture summary & quickstart commands ready to paste into coding agents.
curl -sL https://repoforai.com/repo/statelyai/agent.md
Active README embeds unlock Hot-Tier 6h Telemetry Refresh automatically (zero pay-to-rank).
Frequently Asked Questions about agent
How do I integrate it without handing control flow to the model?
setupAgent, then pass the machine and executor functions to runAgent. The model can propose an event from the allowed set, but the machine still evaluates the transition guard. The host owns credentials, model calls, and snapshot storage; it can resume a paused run by passing a snapshot and an event.Does it need a GPU, and what determines runtime overhead?
Can I use it commercially and self-host it?
When is it a better fit than an AI SDK loop or an existing graph framework?
Upstream README & Documentation
This profile focuses on independent engineering analysis and live telemetry. Read the full upstream README, changelog, and examples directly on GitHub.
Top 10 Breakout Open-Source AI Repos, Every Thursday
Signal-only digest of fast-growing AI repos, SPDX license alerts, and architecture picks. Zero spam.
Zero spam · 1-click unsubscribe · Privacy Policy