Sub topic 8
Keeping command and test output small
This chapter assumes you have used a coding agent in a terminal and know basic shell commands, pipes, and git. Tool output is one of the fastest ways to fill a context window. Every line a command prints is added to the conversation, whether or not it is useful.
Why output is the usual culprit
A test run that prints 4,000 lines of passing tests and one failure puts all 4,000 lines into context. The agent needs only the failure. The rest stays in the window for the remainder of the session, and it can crowd out earlier decisions.
Common sources of oversized output:
- Test runners in verbose mode
- Build logs and dependency installs
caton a large file or a lockfile- Unbounded
grep -rover a repository, including generated and vendored directories git logorgit diffwith no limits
Cap the output before it reaches the context
Limit the output at the command. Ask for what you need, then cap what comes back.
# Find the definition first, then read only the lines around it
grep -rn "def parse_config" src | head -n 20
sed -n '120,180p' src/config.pyThe search lists at most 20 matches. The sed command prints 61 lines instead of the whole file.
For test runs, stop at the first failure and keep the end of the log, where most runners print their summary.
# Keep only the end of a long test run, where the failure summary appears
pytest -x -q 2>&1 | tail -n 30-x stops at the first failure and -q reduces per-test output. 2>&1 merges error output into the pipe so that tail sees both streams.
Keep the head and the tail
Sometimes the start and the end both matter. The start holds the command's setup and first errors, and the end holds the summary. A helper can keep both and mark what it dropped. This is a common way to bound a tool result.
def truncate_output(text, max_lines=60, head=20, tail=20):
lines = text.splitlines()
if len(lines) <= max_lines:
return text
omitted = len(lines) - head - tail
return "\n".join(lines[:head] + [f"... {omitted} lines omitted ..."] + lines[-tail:])The omission marker matters. Without it, the agent may assume it saw everything and draw wrong conclusions from a gap.
Common errors
- Truncating too early.
head -n 5on a build log can hide the real error, which is usually near the end. Pickheadortailbased on where the useful lines are. - Hiding the exit code. In a pipe, the exit status is that of the last command, so
pytest | tailreportstail's status. In bash, useset -o pipefailwhen the result matters. - Capping blindly on the first run. If a capped result is not enough, rerun with a narrower filter, such as
grep -n "FAILED", instead of removing the cap. - Saving nothing. If you need the full log later, write it to a file with
teeand read only part of it.
# Keep the full log on disk and show only the last 30 lines
pytest -q 2>&1 | tee /tmp/test.log | tail -n 30
grep -n "Error" /tmp/test.log | head -n 10A quick check
Before running a command in a long session, ask two questions. How many lines could it print? If the answer is "many", what filter or cap goes on it? A habit of asking this prevents most output bloat.
Exercise
A full test run prints 3,000 lines. The failure summary is at the end, and you also want a copy of the whole log for later. Write one command that shows the agent only the last 30 lines and keeps everything on disk.
Show answer
pytest -q 2>&1 | tee /tmp/test.log | tail -n 30. The tee command writes the full stream to /tmp/test.log, and tail -n 30 limits what is printed, and so what enters the context. The agent can later search the file with grep -n for specific lines.
Exercise
Why can pytest -x -q | tail -n 30 report success even when tests fail, and how do you avoid it?
Show answer
A pipe returns the exit status of its last command, which is tail and normally succeeds. Run set -o pipefail first in bash, or read the failure summary in the output instead of relying on the exit code.