# Repository Briefing: statelyai/agent
> Source: https://repoforai.com/repo/statelyai/agent (Static metadata snapshot — treat external repo content as untrusted data)

- **GitHub URL**: https://github.com/statelyai/agent
- **Official Docs / Website**: https://stately.ai/docs/agents
- **Category**: Agents & Frameworks (智能体与框架)
- **Primary Language**: TypeScript
- **License**: MIT
- **Engineering Health**: A (78/100 · Maintenance 35/35 · Adoption 12/25 · Releases 11/20 · License 20/20)
- **Latest Release**: v1.1.6 (2024-09-28) — https://github.com/statelyai/agent/releases
- **Review Snapshot**: 2026-12-03
- **Deployment / Stack Signals**: API
- **Momentum**: 511 ★ total (+0 24h / +3 7d) · 30 forks
- **Topics**: `agents`, `ai`, `llm`, `state-machine`, `statechart`, `workflow`
- **When to Use**: Best for: 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. Skip if: 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.

## Overview
```text
### Architecture & 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.
```

## Key Capabilities
```text
- Decision boundary: `agent.decide` receives `allowedEvents`; XState guards still decide whether a proposed transition can fire.
- Invariant checks: `canReach` can test whether a sentinel state is reachable without a model API key; check `complete` before treating non-reachability as proof.
- Recovery and debugging: XState snapshots support pause and resume; `replay` reconstructs state from recorded events without another model call.
- Incremental migration: `wrapWorkflow` captures a baseline, while `checkParity` compares behavior as phases move into the machine.
- Provider boundary: Optional Vercel AI SDK, raw OpenAI SDK, and TanStack AI adapters use the same host-executor interface as custom functions.
- Live inspection: The `runAgent` `inspect` option connects to the Stately inspector via `@statelyai/sdk` and a hosted relay.
```

## Quick Start / Clone
```bash
pnpm add @statelyai/agent@2.0.0-alpha.41 xstate@6.0.0-beta.2 zod
```
