Skip to content

Getting Started with Agent Graph ​

In about ten minutes you will have a runnable starter Provider: it validates, passes its graph test, returns a Route for the Agent's next step, records a Run and builds into a relocatable bundle you can ship inside your own package.

This tutorial suits creating a new Skill or workflow. Running the commands creates a Provider development project; it does not by itself integrate a Skill or change how any Agent works. If you are improving an existing Skill or embedding Agent Graph in a product, pick that path in What Is Agent Graph first.

You need Node.js 20 or later. Every command runs the latest CLI through npx @c4a/agent-graph@latest, so nothing needs installing first.

1. Create a Provider ​

bash
npx @c4a/agent-graph@latest init ./example-provider --id example-provider
cd example-provider

The generated project is a complete, minimal Provider:

FileWhat it is
provider.yamlThe Provider manifest: ID, version, Graph list and Code Catalog
graphs/main.yamlOne Graph, entry default, an Action node work and a Terminal done
actions/work.yamlAn Agent Action (runner: agent, effect: write) bound to the Skill below
skills/getting-started/SKILL.mdA thin Skill whose metadata binds Provider, Graph main and Entry default
resources/procedure.mdThe procedure the Route requires the Agent to read
codes.yamlThe Code Catalog declaring the reason code route.main.work
tests/main.yamlOne graph test asserting the starter Route is actionable

No runtime directory is created. Where a host keeps Run files is the host's choice; the protocol reserves no directory.

2. Validate and test ​

bash
npx @c4a/agent-graph@latest validate --format json
npx @c4a/agent-graph@latest test tests --format json

Validation checks JSON Schemas, that relative paths stay inside the Provider, IDs and references, entrypoints, explicit Terminals, edge endpoints, Subgraph calls, recursion, dynamic resource materializers and duplicate identities. It never executes an Action. For the starter it reports "state":"valid" with 1 Graph, 2 nodes and 1 edge; the test run reports "state":"passed","total":1,"failed":0.

3. Find the current Route ​

bash
npx @c4a/agent-graph@latest evaluate main --format json

The Evaluation contains a stable statusCode, a revision digest, one primaryRoute, compact alternatives and diagnostics. For the starter, the primary Route is the work node with reasonCode: route.main.work, availability: immediate and the catalog hint "The requested work is ready to perform." No Skill or procedure body is inlined.

Resolve that Route using both values from the same Evaluation:

bash
npx @c4a/agent-graph@latest route main <route-id> --revision <revision> --format json

The Route returns the selected node and its call path, the Action, an empty commandPlan (an Agent Action runs no command), the required resources — the Skill and resources/procedure.md, each as an absolute file path with a content digest and readState: read-required — and afterAction: {evaluate: true, recordNode: "main/work"}. If a Provider file or input changed in between, the CLI rejects the stale revision instead of returning a mismatched plan.

Because the Skill already binds Provider, Graph and Entry, the same Evaluation works from the Skill alone:

bash
npx @c4a/agent-graph@latest --skill ./skills/getting-started/SKILL.md evaluate --format json

4. Track a long task with a Run ​

bash
npx @c4a/agent-graph@latest run start main --state ./runtime/run.json --workspace "$PWD"
# ... the Agent reads the required resources and does the work ...
npx @c4a/agent-graph@latest run record main/work completed --state ./runtime/run.json
npx @c4a/agent-graph@latest run status --manifest ./provider.yaml --state ./runtime/run.json --format json

Record with the exact afterAction.recordNode key. run record reminds you that recorded Outcomes are not proof of Facts, and run status evaluates again: for the starter it now returns statusCode: complete with outcome: completed. In a real Graph a historical completed does not override a missing fact check; the next Evaluation may route to verification or recovery instead. See Runs and Recovery.

5. Build a relocatable bundle ​

bash
npx @c4a/agent-graph@latest build ./dist/provider --format json
npx @c4a/agent-graph@latest --manifest ./dist/provider/manifest.json validate

The bundle contains only reachable Provider files, a normalized provider.yaml and a manifest.json with every file digest, the Graph dependency index and an overall digest. Tests stay out, and no source-machine absolute path is written, so the bundle can be shipped inside your own package.

Next steps ​

  • Turn the starter into a real workflow with Facts, a Gate, recovery and a loop: Authoring Graphs.
  • Cover every branch in CI and ship the bundle: Testing and Publishing.
  • Learn the evaluation model behind what you just ran: Core Model.
  • Browse runnable patterns in the repository's examples/ directory, such as review-gate, facts-recovery, fact-driven-batch, monitoring-loop and subgraphs.

Agent Graph 快速上手 ​

大约十分钟后,你会得到一个可运行的入门 Provider:它能通过校验和 Graph 测试,能为 Agent 的下一步返回 Route,会记录一次 Run,并能构建成可随你自己的包一起发布的可迁移产物包。

本教程适合新建 Skill 或工作流。执行这些命令只会创建一个 Provider 开发项目,本身不会接入任何 Skill,也不会改变 Agent 的工作方式。如果你要改进现有 Skill,或在产品中嵌入 Agent Graph,请先在 Agent Graph 是什么 中选择对应路径。

你需要 Node.js 20 或更高版本。每条命令都通过 npx @c4a/agent-graph@latest 运行最新版 CLI,因此无需事先安装任何东西。

1. 创建 Provider ​

bash
npx @c4a/agent-graph@latest init ./example-provider --id example-provider
cd example-provider

生成的项目是一个完整而最小的 Provider:

文件说明
provider.yamlProvider 清单:ID、版本、Graph 列表和 Code Catalog
graphs/main.yaml一个 Graph,入口为 default,包含 Action 节点 work 和 Terminal(终止节点)done
actions/work.yaml一个 Agent Action(runner: agent、effect: write),绑定到下面的 Skill
skills/getting-started/SKILL.md一个轻量 Skill,其元数据绑定 Provider、Graph main 和 Entry default
resources/procedure.mdRoute 要求 Agent 读取的流程
codes.yaml声明原因码 route.main.work 的 Code Catalog
tests/main.yaml一个 Graph 测试,断言入门 Route 处于可执行状态

不会创建任何运行时目录。Run 文件存放在哪里由宿主决定;协议不预留任何目录。

2. 校验与测试 ​

bash
npx @c4a/agent-graph@latest validate --format json
npx @c4a/agent-graph@latest test tests --format json

校验会检查 JSON Schema、相对路径是否留在 Provider 内、ID 与引用、入口、显式的 Terminal、边的端点、Subgraph 调用、递归、动态资源物化器以及重复的身份标识。它从不执行 Action。对于入门项目,它报告 "state":"valid",包含 1 个 Graph、2 个节点和 1 条边;测试运行报告 "state":"passed","total":1,"failed":0。

3. 找到当前 Route ​

bash
npx @c4a/agent-graph@latest evaluate main --format json

Evaluation 包含一个稳定的 statusCode、一个 revision 摘要、一个 primaryRoute、精简的备选项和诊断。对于入门项目,主 Route 是 work 节点,带有 reasonCode: route.main.work、availability: immediate 以及目录提示 “The requested work is ready to perform.”。不会内联任何 Skill 或流程正文。

使用同一次 Evaluation 中的两个值来解析该 Route:

bash
npx @c4a/agent-graph@latest route main <route-id> --revision <revision> --format json

Route 返回选中的节点及其调用路径、Action、一个空的 commandPlan(Agent Action 不运行命令)、必需的资源——Skill 和 resources/procedure.md,每项都以绝对文件路径给出,带有内容摘要和 readState: read-required——以及 afterAction: {evaluate: true, recordNode: "main/work"}。如果期间某个 Provider 文件或输入发生了变化,CLI 会拒绝这个过期的修订号(revision),而不是返回一个不匹配的计划。

由于 Skill 已经绑定了 Provider、Graph 和 Entry,仅凭 Skill 也能完成同样的 Evaluation:

bash
npx @c4a/agent-graph@latest --skill ./skills/getting-started/SKILL.md evaluate --format json

4. 用 Run 跟踪长任务 ​

bash
npx @c4a/agent-graph@latest run start main --state ./runtime/run.json --workspace "$PWD"
# ... the Agent reads the required resources and does the work ...
npx @c4a/agent-graph@latest run record main/work completed --state ./runtime/run.json
npx @c4a/agent-graph@latest run status --manifest ./provider.yaml --state ./runtime/run.json --format json

记录时使用 afterAction.recordNode 给出的确切键。run record 会提醒你,记录的 Outcome 并不能证明 Facts;run status 会再次评估:对于入门项目,它现在返回 statusCode: complete 和 outcome: completed。在真实的 Graph 中,历史上的 completed 不能覆盖缺失的 Fact 检查;下一次 Evaluation 可能会转向校验或恢复。见 运行与恢复。

5. 构建可迁移的产物包 ​

bash
npx @c4a/agent-graph@latest build ./dist/provider --format json
npx @c4a/agent-graph@latest --manifest ./dist/provider/manifest.json validate

产物包只包含可达的 Provider 文件、一个规范化的 provider.yaml,以及一个 manifest.json,其中记录每个文件的摘要、Graph 依赖索引和整体摘要。测试不会被打包,也不会写入源机器上的任何绝对路径,因此产物包可以随你自己的包一起分发。

下一步 ​

  • 把入门项目扩展为带有 Facts、Gate、恢复和循环的真实工作流:编写 Graph。
  • 在 CI 中覆盖每个分支并分发产物包:测试与发布。
  • 了解你刚才运行的内容背后的评估模型:核心模型。
  • 浏览仓库 examples/ 目录中可运行的模式,例如 review-gate、facts-recovery、fact-driven-batch、monitoring-loop 和 subgraphs。