Running Agency code
To compile and run an Agency file in one step:
agency run foo.agencyThis compiles the file and then executes its main node.
This is also the default if no command is specified:
agency foo.agencyThe shorthand takes the same options and splits its command line the same way, so agency greet.agency --name alice works exactly like the run form below.
This compiles the file to JavaScript and immediately executes it under the same Node binary that is running the CLI. Use the separate agency resume command to continue from a saved checkpoint.
Options
--trace— write an execution trace as the program runs, to<input>.trace. See traces and bundles for what you can do with a trace file.--trace-file <path>— write the execution trace to this path instead.--max-cost <dollars>— abort the run if its LLM spend exceeds this many dollars, e.g.--max-cost 0.50.0means no paid spend at all (local models only). A negative value means no limit. A tripped budget exits with code 3 and prints the overrun.--max-time <duration>— abort the run if its working time exceeds this duration, e.g.--max-time 5m. The value needs a unit:500ms,30s,5m,1h,2d,1w. Time spent waiting on a human does not count. Zero or negative means no limit. A tripped budget exits with code 3.--model <name>— the model this run'sllm()calls use by default, written asmodelorprovider/model. See choosing a model below.-i,--interactive— prompt at the terminal for an interrupt that nothing in the program or its policy answered. See the policies guide.--wait-for-response— keep running and wait foragency attachto answer such an interrupt. See waiting for an answer below. It cannot be combined with-i.--name <name>— the name this run shows inps, asagency run <name>. The default is the input file's name.--detach <output-file>— start the program in the background, save what it prints to this file, and return at once. See running in the background below. It cannot be combined with-ior--capture-workdir.--messages <file>— start the program with a conversation already on its first thread. See starting with a conversation below.
Waiting for an answer
A program sometimes raises an interrupt that none of its handlers answers and that its policy has no rule for. What happens next depends on how you started the program:
| Flags | What happens to the interrupt |
|---|---|
| No policy flag at all | The program reports the interrupt and exits. |
--policy, --approve, or --reject | It is rejected. |
--interactive | You are prompted at the terminal. With no terminal, it is rejected. |
--wait-for-response | The program keeps running and holds the question until someone answers it. |
--wait-for-response is for a program with no terminal of its own: one you started under nohup, in tmux, or from a scheduler. The program prints one line and waits:
$ agency run --wait-for-response agent.agency
[agency] Waiting for an answer to 1 question. Run: agency attach 48213You answer from any other terminal with agency attach 48213.
A program run this way never approves or rejects a question by itself, however long it waits. Time spent waiting does not count toward --max-time.
The flag works together with a policy. The policy answers what it has rules for, and only the rest is held:
agency run --wait-for-response --policy recommended agent.agencyOne case is worth knowing. An interrupt that asks for a value, such as question(), is held even when an approve rule matches it. A person can supply the value through agency attach, and a rule cannot.
A program that waits has paused its run, so any background jobs it started have stopped.
Saving a checkpoint while waiting
A program that holds a question keeps it in memory. If the process dies, the question and the run are lost.
You can ask Agency to also save a checkpoint to disk while the program waits. Turn the setting on in agency.json:
{
"spawn": {
"interrupts": { "preemptivelyCreateCheckpoint": true }
}
}It is off by default. A checkpoint holds the program's whole state, including any secret a variable holds, and Agency does not write that to disk unless you ask.
With the setting on, the checkpoint goes under ~/.agency/held/, in a file only you can read. The program prints the command that resumes from it:
[agency] Waiting for an answer to 1 question. Run: agency attach 48213
If this process stops, resume it with: agency resume /home/me/.agency/held/Xq3f-48213.json <program> --wait-for-response --policy /home/me/.agency/held/Xq3f-48213.policy.json --max-cost 2A checkpoint does not hold the program's policy or its cost and time limits, so the command carries them. If you started the program with --policy, --approve, or --reject, Agency saves the resulting policy in a second file beside the checkpoint and names it in the command. Keep the flags when you resume: without them the program comes back with no rules and no limits.
Agency deletes both files once the question is answered. If the process dies first, the files stay. Resuming raises the same interrupt again, so the program's handlers and policy are consulted again, and the resumed program holds the question if they still do not answer it. Agency does not delete the files after a resume, so remove them when you no longer need them.
Running in the background
--detach starts the program and returns at once. The program keeps running after the command exits, and after you close the terminal:
$ agency run --detach report.log --wait-for-response report.agency
report.agency → report.js (in 187.79ms)
{"pid":48213,"outputFile":"/home/me/project/report.log"}The last line is JSON, so a script can read the process id from it.
- Output. Everything the program prints, and its errors, are appended to the file you named. You always name the file: Agency does not pick a place to save a program's output for you. A new file is readable by you alone. The program stops saving at 10 MiB per stream and keeps running.
- Finding it. The program shows in
psasagency run report.agency, or asagency run <name>when you pass--name. Stop it withkill 48213. - Ctrl-C. The program is in its own process group, so Ctrl-C in your terminal does not reach it.
- Interrupts. A detached program has no terminal to prompt at. Add
--wait-for-response, as above, and answer its questions withagency attach 48213, which also prints the program's output.
agency resume takes --detach too.
The output file cannot be a symbolic link.
To start a background program from Agency code, use spawn, which runs this command for you.
Starting with a conversation
--messages puts a list of messages on the program's first thread before the program runs. The program's first llm() call then continues that conversation.
agency run --messages history.json assistant.agencyThe file holds a JSON list of messages:
[
{ "role": "user", "content": "What is the capital of France?" },
{ "role": "assistant", "content": "Paris." }
]Agency reads the file and leaves it where it is.
Only the first thread is seeded. A thread { } block the program opens still starts empty.
A conversation copied while a model was in the middle of calling a tool ends with a tool call that has no result, and a model provider refuses a conversation in that state. So Agency drops any tool call in the last assistant message that has no result. Nothing else in the list is changed.
With --detach, the list can come from standard input, written as -:
cat history.json | agency run --detach out.log --messages - assistant.agencyagency resume does not take --messages. A resumed program's threads come from its checkpoint.
Resuming a checkpoint
Use the checkpoint JSON and the same Agency source file:
agency resume checkpoint.json foo.agencyresume accepts the same model, policy, budget, trace, strictness, --agency-only, and capture options as run. It also accepts repeatable value overrides:
agency resume checkpoint.json foo.agency \
--local-var mood='"happy"' \
--arg input='{"retry":true}' \
--global-var attempts=8 \
--program-arg helloEach name=value value is parsed as JSON when possible. Other values remain strings. --program-arg is separate because process arguments are not saved in a checkpoint.
The checkpoint can be edited in other ways too, the same ways the debugger offers at a stop: a message appended to a thread, a tool removed from the model's next round, a guard disarmed or given a new limit, a branch given its result. Each is one --edit with the edit as JSON, or a --edits file holding a list of them, applied in order after the overrides above:
agency resume checkpoint.json foo.agency \
--edit '{"op":"appendMessage","text":"Stop research now. Write up what you have."}' \
--edit '{"op":"removeTool","tool":"search"}'A wrong shape is refused before the program starts. The edits and their addresses are listed in the debugger page. Signed checkpoints are verified automatically; use --force to resume one whose signature cannot be verified. See Checkpointing for creating the JSON file and the integrity rules.
Choosing a model
--model sets the model for a run without editing any files, which is handy for comparing two models against the same program:
agency run --model claude-opus-4-8 greet.agency
agency --model claude-opus-4-8 greet.agencyIt only changes the default. A model chosen in your Agency code still wins, whether that is setModel() from std::llm or a single llm(prompt, { model: "..." }) call. The flag is the value used when nothing else says otherwise.
Naming a provider
Most of the time the model name is enough — agency looks it up and knows which provider it belongs to. When it isn't, put the provider first, separated by a slash:
agency run --model anthropic/claude-opus-4-8 greet.agency
agency run --model openrouter/anthropic/claude-sonnet-4 greet.agency
agency run --model my-company/my-fine-tune greet.agencyOnly the first slash separates the two parts. That matters for OpenRouter, whose model names contain a slash of their own: in the second example the provider is openrouter and the model is anthropic/claude-sonnet-4.
Any name works as a provider, not just the built-in ones, so a provider you registered yourself with providerModules works the same way.
A bare name clears a configured provider
If your agency.json sets client.defaultProvider, a bare --model clears it and lets agency work the provider out from the model name. That is what "just give me this model" means, but it is worth knowing if you use a provider to route requests somewhere specific:
# agency.json says defaultProvider is "litellm"
agency run --model gpt-4o-mini greet.agency # goes straight to OpenAI
agency run --model litellm/gpt-4o-mini greet.agency # keeps your proxyNaming the provider keeps it.
Unknown names
A bare name has to be one agency knows about, so a typo is caught before anything runs rather than after your program has already done work:
$ agency run --model gpt-4o-minii greet.agency
error: option '--model <name>' argument 'gpt-4o-minii' is invalid. Unknown model "gpt-4o-minii".
Did you mean "gpt-4o-mini"?
For a model from another provider, write provider/model — e.g. openrouter/gpt-4o-minii.
Run `agency models list` to see the catalog.A name with a provider in front is never checked, because agency has no way to know what models your provider offers. So if you want a model agency hasn't heard of — a brand new release, or anything behind a provider of your own — put the provider in front and it will be passed through untouched.
A note on global installs
If you have installed agency globally, you should be aware of a classic node gotcha. A global install means that the agency CLI will be available everywhere. However, the agency-lang package can't be imported everywhere. This matters because when you compile your agency code into js, the js code imports the agency-lang package.
This means you may run into some very annoying behavior, where compiling the code is just fine
agency compile foo.agencyBut when you try to run it
node foo.jsyou get an error that looks something like this:
node:internal/modules/package_json_reader:316
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'agency-lang' imported from /Users/foo/hello.js
at Object.getPackageJSONURL (node:internal/modules/package_json_reader:316:9)
at packageResolve (node:internal/modules/esm/resolve:768:81)
at moduleResolve (node:internal/modules/esm/resolve:858:18)
at defaultResolve (node:internal/modules/esm/resolve:990:11)
at #cachedDefaultResolve (node:internal/modules/esm/loader:737:20)
at ModuleLoader.resolve (node:internal/modules/esm/loader:714:38)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:293:38)
at #link (node:internal/modules/esm/module_job:208:49) {
code: 'ERR_MODULE_NOT_FOUND'
}If you're in an NPM project, simply install the agency-lang package locally, and this problem will go away, but if you are trying to run an agency agent as a script, just use the run command. The run command will tell node how to find the globally installed agency-lang package.
tl;dr if compile-then-run doesn't work:
agency compile foo.agency
node foo.jsUse the run command instead:
agency run foo.agencyYou can also use pack to produce a standalone script that has no dependencies at all. It inlines the agency package instead of importing it, so it will run anywhere with just Node installed.
Passing arguments to your program
Arguments after the file are passed through to your program, and you read them with std::args:
agency run greet.agency --name alicePosition decides who a flag belongs to. Agency's own flags go before the filename; everything after it is your program's:
agency run --policy strict greet.agency --name alice
# agency's your program'sThis is the rule node uses, in node --inspect script.js --verbose.
The entry node's parameters are not filled from the command line. A parameter you do not supply another way is undefined, and its default applies if it has one.
Position always decides, even when the flag is one agency also defines. Writing agency run greet.agency --max-cost 5 sends --max-cost 5 to your program and does not cap the run's spend. Since that is easy to do by accident, agency says so:
$ agency run greet.agency --max-cost 5
Warning: --max-cost went to your program, not to agency.
Agency flags go before the filename: agency run --max-cost ... greet.agency
Write -- before it to silence this: agency run greet.agency -- --max-cost ...If your program really does own a flag by that name, the second line silences the warning. Nothing else needs --.
A compiled or packed program works the same way, since there is no agency command line to separate from:
agency compile greet.agency
node greet.js --name aliceDo not carry a -- over to that form. std::args reads -- as "stop reading flags", so node greet.js -- --name alice leaves name at its default and puts --name and alice in positionals.
Note when invoking through another tool: npx agency run file.agency -- x loses the -- to npx. It works when invoking the agency binary directly.
This applies to agency eval run --agent-cmd too. A command like agency run writer.agency {task} delivers the task through the program's argv, so the agent reads it with args() from std::system rather than declaring a parameter on main.