Sub topic 4
Writing prompts that never need to ask a question
This chapter assumes you can run a coding agent non-interactively and have read the earlier chapters on headless mode and scheduler environments. You should be comfortable with shell scripts and Git. Here you will learn how to write the prompt itself, so the agent can finish a task with nobody present to answer it.
Why interactive prompts fail unattended
In an interactive session, a vague prompt is cheap. The agent asks "Which branch?" or "Should I delete this file?", and you answer. In a headless run, nobody is there to answer. The agent has three bad options:
- Guess, and possibly act on the wrong assumption.
- Stop and wait, which can hang until the timeout kills the job.
- Exit early with no useful output.
A self-contained prompt removes the reasons to ask. It states the goal, the context, the permitted actions, what to do when something is unclear, and what a finished run looks like.
The last branch is the one you are designing away. Every decision point should end in the top three outcomes.
The parts of a self-contained prompt
A reliable unattended prompt has six parts.
- Goal. One or two sentences about the outcome, not the method.
- Context. Where things are: the working directory, the files that matter, and the tools installed. Do not rely on the agent to discover these.
- Allowed actions. An explicit list of what the agent may read, run, and write.
- Uncertainty rules. What to do when something is ambiguous or broken.
- Output contract. Which file to write and what shape it has.
- Done condition. How the agent knows to stop.
Goal and context
Describe the result you want and the boundary of the job. "Find outdated direct dependencies and write a report. Do not change any dependency files" works better than "check the dependencies". The second version leaves open whether the agent should upgrade anything.
State context as facts. A scheduler gives the agent a different working directory and environment than your shell does, so name paths relative to a known root and say what that root is.
Allowed actions
Tell the agent which commands it may run and where it may write. This is guidance for the model, not enforcement. The enforcement comes from the permissions you configure for the agent, which a later chapter covers. If the prompt and the permissions disagree, the permissions win, and the agent may stall on a denied action. Keep the two lists identical.
Uncertainty rules
This is the part most people skip. Write rules that cover three situations:
- Ambiguity. "If two interpretations are possible, choose the more conservative one and record it under
assumptions." - Failure. "If a required command fails, do not look for a workaround. Record the error and stop."
- Expected non-zero exits. Some tools use exit codes as results. For example,
npm outdatedexits with status 1 when it finds outdated packages. Without a note, an agent may treat that as a failure and start debugging.
Also say plainly that nobody will answer questions. Agents often behave better when told that asking is not available.
Output contract and done condition
Name the output file and its shape. Structured output and its validation get their own chapter, so here you only need to say that the shape is fixed and the file path is exact. Finish with a done condition such as "You are finished when reports/deps.json exists and contains valid JSON. Then stop." Without it, an agent may keep exploring and spend budget.
A complete example
This prompt file belongs at prompts/nightly.md in the repository.
# Task: nightly dependency report
## Goal
Find outdated direct dependencies of this project and write a report.
Do not modify any dependency or lock file.
## Context
- The working directory is the repository root.
- Direct dependencies are listed in package.json.
- The "Run facts" section at the end of this prompt gives today's date and the current commit.
## Allowed actions
- Read any file in the repository.
- Run `npm outdated --json`. It exits with status 1 when packages are outdated. That is a normal result, not an error.
- Write only to `reports/deps.json`.
## When something is unclear
- If a choice is ambiguous, pick the more conservative option and add one sentence to the "assumptions" list in the report.
- If a required command fails for any other reason, do not look for a workaround. Put the error text in the "errors" list in the report and stop.
- Do not ask questions. No one is available to answer.
## Output
Write `reports/deps.json` as a JSON object with these keys:
- "run_date": the date from Run facts
- "commit": the commit from Run facts
- "outdated": a list of objects with "name", "current", and "latest"
- "assumptions": a list of strings
- "errors": a list of strings
## Done when
`reports/deps.json` exists and is valid JSON. Then stop.Injecting facts at run time
The prompt file is static, but some facts change on every run: the date, the commit, the branch. The agent can look these up, but each lookup is another place for it to go wrong. A wrapper can append them as a "Run facts" section instead.
#!/usr/bin/env bash
# build-and-run.sh
# AGENT_CMD is a placeholder for your agent's non-interactive command.
# This script assumes it reads the prompt from stdin; check your agent's documentation.
set -eu
: "${AGENT_CMD:?set AGENT_CMD to your agent's non-interactive command}"
cd /srv/repos/my-project
prompt_file="$(mktemp)"
trap 'rm -f "$prompt_file"' EXIT
{
cat prompts/nightly.md
printf '\n## Run facts\n'
printf -- '- Run date (UTC): %s\n' "$(date -u +%Y-%m-%d)"
printf -- '- Current commit: %s\n' "$(git rev-parse --short HEAD)"
printf -- '- Current branch: %s\n' "$(git rev-parse --abbrev-ref HEAD)"
} > "$prompt_file"
# AGENT_CMD is intentionally unquoted so a command with arguments splits into words.
timeout 20m $AGENT_CMD < "$prompt_file"Because the script exits with the status of the last command, timeout still reports 124 on expiry, as covered in the timeouts chapter.
Versioning the prompt file in the repository also helps. A change to the prompt shows up in review, and the commit that produced a bad run is easy to find.
Interactive habits and their headless rewrites
| Interactive habit | Why it fails unattended | Headless rewrite |
|---|---|---|
| "Fix the failing tests" | Which tests, which branch, and what counts as fixed are all open | "Run `npm test`. For each failure, write the test name and error to the report. Do not edit files." |
| "Use your judgment" | The agent has no one to check against | "If unsure, take the more conservative option and record it." |
| "Let me know if you need anything" | No one is listening | "Do not ask questions. Record blockers in `errors` and stop." |
| "Look at the latest issue" | The latest changes between runs | Pass the issue number in the run facts. |
| "Clean up when done" | Cleanup may delete things you wanted | List the exact files that may be deleted, or leave cleanup out. |
Edge cases
- Contradictory instructions. If the goal says "do not modify files" and a later step says "update the lock file", the agent must choose. Read the prompt once as a skeptic and look for conflicts.
- Prompt length. A longer prompt is not a safer one. Cut background the agent does not need. Every sentence is something that can conflict with another.
- Partial success. Decide in advance whether a run that completed half the work counts as success. Tell the agent to record partial results and then exit, and decide in the wrapper how to treat them.
- Run-time data in the prompt. Anything you paste into the prompt from outside, such as an issue body or a commit message, can contain instructions. Pass such data as clearly labeled content, and never let it sit where the agent can mistake it for your rules. The prompt injection chapter covers this in depth.
Checklist
Before scheduling a prompt, confirm that it:
- States a goal that is narrow enough to finish in one run.
- Names the working directory and key files.
- Lists the allowed commands and write locations, matching the configured permissions.
- Has rules for ambiguity, failure, and expected non-zero exits.
- Fixes the output path and shape.
- Ends with a done condition.
Exercise
Your prompt says "Update the changelog for the latest release." The job hangs until the timeout. Name two gaps in this prompt that could cause a question, and rewrite the prompt so neither can.
Show answer
The prompt leaves "latest release" undefined, so the agent may ask which tag or version. It also does not say where the changelog is or what to do if the entry already exists. A rewrite: "The changelog is CHANGELOG.md in the repository root. The release to document is the tag given under Run facts. Read the commit messages since the previous tag and add one entry under a heading for that tag. If an entry for that tag already exists, make no changes and record 'entry exists' in reports/changelog.json. If the tag is missing from Run facts, record the error and stop. Do not ask questions. You are finished when CHANGELOG.md is updated or the report records why it was not."