Topic 5 of 5 · 4 min read
Logging, limits and telemetry for unattended runs
Goal: Set turn, time and spend limits for an unattended agent and read its telemetry to tell a stalled run from a working one.
An unattended agent needs three things before you walk away: hard limits on turns, time and spend, a saved record of what it did, and a signal you can read to tell a working run from a stuck one.
Check yourself · 1 of 3
Why set a turn cap on an unattended agent?
Why it matters
An agent in a loop can keep calling tools and the model for as long as you let it. A confused run might retry the same failing command for an hour. Nobody sees it, and the bill and the runner time grow. When something goes wrong at 3 a.m., you also need to answer "what did it do?" without rerunning it.
How to do it
- Cap turns. Claude Code print mode has
--max-turns, which ends the run after that many agent turns. Pick a number near twice what a normal run needs. - Cap spend. Claude Code has
--max-budget-usdfor a dollar ceiling on a run. For tools without it, count tokens in your wrapper and stop the process when you pass your limit. - Cap time. Wrap the call in
timeoutand set the CI job's own timeout as a backstop. Stop the process by its process id, not by name. - Save the transcript. Write the JSON or stream output to a file and keep it as a CI artifact or a log object. Include the prompt, the tool calls, the exit code and the session id. Remove secrets before storing.
- Emit telemetry. Claude Code can export OpenTelemetry metrics and events when you set
CLAUDE_CODE_ENABLE_TELEMETRY=1and an exporter, for exampleOTEL_METRICS_EXPORTER. Metrics include token usage and cost. Send them to the collector you already run. - Alert on the right things. Failed runs, runs that hit a cap, cost per run above a threshold, and no events for longer than the longest normal quiet period.
Telling stalled from working
- Working: new tool-call events keep arriving, token counters rise, the transcript file grows.
- Stalled: no new events and a flat token count while the process is still alive, or the same tool call repeated with the same error.
- Looping: events keep coming, but they repeat. Turn cap and budget cap are what end this, so they must be set.
timeout 900 claude -p "Triage the failing tests in tests/." \
--max-turns 25 --max-budget-usd 3 \
--output-format stream-json > "logs/run-$(date +%s).jsonl"
echo "exit=$?" >> logs/summary.txt
Before and after
Weak: a nightly agent with no caps and output sent to /dev/null. A bad night costs real money and leaves no trace.
Better: caps on turns, spend and time, the stream saved as an artifact, and an alert when a run ends by hitting a cap.
What goes wrong
- Limits set too tight. Every run fails at the cap. Check normal runs first.
- Logs with secrets. Tool output can include tokens or customer data.
- Alerting only on failure. A run that "succeeds" after a hundred retries is also a problem.
- No process cleanup. After a stall, confirm no child processes remain.
Examples
The silent retry loop
A nightly agent retries a failing migration command for four hours because no turn cap is set. The saved transcript shows the same error forty times. Adding --max-turns 25 and an alert on cap hits would have ended it in minutes.
Reading a dashboard
A run's token counter stopped rising twenty minutes ago and no tool events arrived, yet the process is alive. That pattern points to a stall, so the wrapper stops the process by its id and marks the run failed.
Key points
- Set caps on turns, time and spend for every unattended run, and a CI job timeout as a backstop.
- Claude Code offers --max-turns and --max-budget-usd; wrap other tools with timeout and your own token count.
- Save the transcript, tool calls, exit code and session id, and strip secrets before storing it.
- OpenTelemetry metrics and events show token use, cost and activity for runs you cannot watch.
- A working run keeps producing events and tokens, while a stalled run goes quiet and a looping run repeats.
Common mistakes
- Running unattended with no turn, time or spend cap.
- Discarding output instead of saving the transcript as an artifact.
- Storing transcripts without removing secrets or customer data.
- Alerting on failures only and missing runs that hit a cap or cost far more than usual.
Check yourself · 2 of 3
A run's process is alive, but no tool events have arrived and token usage is flat for twenty minutes. What is the likely state?
Check yourself · 3 of 3
What should you do before storing an agent transcript as a CI artifact?
Practice items, not counted towards your mastery.
Start here
- Run Claude Code programmatically (Claude Code docs) (opens in a new tab)DocsFree
Print mode, output formats, JSON schemas, allowed tools and session resume for scripted runs.
- Non-interactive mode (Codex docs) (opens in a new tab)DocsFree
How codex exec runs in scripts and CI, with JSON events, output schemas and sandbox settings.
- Claude Code GitHub Action (repository) (opens in a new tab)ToolFree
A real agent action for GitHub: triggers, inputs and permissions you can read and copy.
More for this topic
- Monitoring usage (Claude Code docs) (opens in a new tab)DocsFree
OpenTelemetry settings, metrics such as token and cost usage, and events for agent runs.