Sub topic 15
Running agents from scripts
This chapter assumes you can write shell scripts, know how exit codes and pipes work, and have read the chapters on permissions and configuration files. An interactive session needs a person to answer approval prompts. A script has no person, so you must decide in advance what the agent may do.
One prompt, no session
Each tool can run a single prompt, print the result, and exit. This is called non-interactive mode, and it lets you call an agent from scripts and pipelines.
claude -p "Explain what the build script in package.json does"
codex exec "Explain what the build script in package.json does"
copilot -p "Explain what the build script in package.json does"The agent still runs its loop of model call, tool call, and observation. It just stops after the final answer instead of waiting for your next message.
Pipelines and output
The agent writes its answer to standard output, so you can redirect it or pipe it like any other command. Many tools also read standard input, which lets you pass data in. Check each tool's --help for the exact flags, because they change between versions.
#!/usr/bin/env bash
set -euo pipefail
git diff main...HEAD > /tmp/changes.diff
claude -p "Summarize the risks in the diff at /tmp/changes.diff in five bullet points" \
> review-notes.md
echo "Wrote review-notes.md"Permissions without a human
A prompt that waits for approval will hang or fail in a script. Do not fix this by turning every check off. Instead, pre-approve the narrow set of actions the job needs, and deny the rest.
- In Claude Code, put allow and deny rules in
.claude/settings.json, as shown in the configuration chapter. - In Codex CLI, set the approval policy and sandbox mode in
~/.codex/config.toml. - Prefer a read-only or workspace-only sandbox for scripted runs. A summary job needs no write access.
Treat output as untrusted
A scripted agent's output is text from a language model. It can be wrong, and it can be influenced by whatever files it read. Do not pipe it straight into sh, and do not commit it without a check. Keep the safety nets from earlier chapters: run on a branch, then run tests and read the diff.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Script hangs | Agent is waiting for an approval | Add an allow rule, or use a read-only task |
| Not logged in | No credentials in the CI environment | Provide authentication through the tool's documented environment variable or login step |
| Different answers each run | Model output varies | Make the prompt specific and verify with tests |
| Unexpected edits | Prompt too broad or sandbox too loose | Narrow the task and restrict writes |
Exercise
You want a nightly job that asks an agent to summarize new commits. Which permission setup fits best, and why should the job's output not be run as a shell command?
Show answer
Use a read-only or workspace-only sandbox with no write or network approvals, because summarizing needs only reading. The output is model-generated text that can be wrong or influenced by malicious file contents, so running it as a command could cause harm. Treat it as data and have a human review it.