[MODEL] Claude pipes Bash output through tail/head, discarding errors before reading them

Resolved 💬 5 comments Opened Mar 27, 2026 by carrotRakko Closed Jun 1, 2026

Preflight Checklist

  • [x] I have searched existing issues for similar behavior reports
  • [x] This report does NOT contain sensitive information (API keys, passwords, etc.)

Type of Behavior Issue

Other unexpected behavior

What You Asked Claude to Do

Run long-running commands (e.g., npm test, npm run build, code generation scripts) and verify success/failure of each.

What Claude Actually Did

Instead of writing command output to a file and then inspecting it, Claude consistently pipes output through | tail -N (e.g., node generate-files.js 2>&1 | tail -3), discarding the beginning of the output before ever reading it.

This caused Claude to miss a script crash entirely — the script threw a ReferenceError and exited with a non-zero code, but | tail -3 only showed the last 3 lines (which looked benign), so Claude reported success.

This pattern reproduced across multiple separate sessions with different Claude Code instances (Opus 4.6):

  • In one session, npm test output was piped through tail → Claude read partial results and reported "all tests pass" before tests had finished
  • In the next session, a code generation script output was piped through | tail -3 → script crashed with ReferenceError, Claude reported success because the last 3 lines didn't show the error

Both instances involved the same anti-pattern: discarding output before reading it to save context tokens.

Expected Behavior

Claude should write command output to a file first, then inspect the file:

command > /tmp/result.log 2>&1
echo "exit: $?" >> /tmp/result.log

Then check exit code with tail -1 /tmp/result.log, and if needed, investigate with grep or cat.

The Bash tool already has a persisted-output mechanism that saves large outputs to files automatically. Claude should not preemptively discard output with pipes — the tooling handles large output gracefully.

Files Affected

N/A (behavioral pattern, not file modification)

Permission Mode

Accept Edits was ON (auto-accepting changes)

Can You Reproduce This?

Yes, every time with the same prompt

Steps to Reproduce

  1. Ask Claude Code to run a command that produces substantial output (e.g., npm test, npm run build, or any build/generate script)
  2. Observe that Claude wraps the command with | tail -N or | head -N
  3. If the command fails with an error in the truncated portion, Claude will report success

This is especially likely when Claude has been working for a while and is (presumably) trying to conserve context tokens.

Claude Model

Opus

Relevant Conversation

Claude's internal reasoning (chain-of-thought) reveals the motivation:

"Output looks large, let me save context"

The model explicitly optimizes for context conservation at the expense of correctness. It decides to truncate before seeing the output, making it impossible to detect errors in the discarded portion.

Impact

Medium - Extra work to undo changes

Claude Code Version

2.1.81 (Claude Code)

Platform

Anthropic API

Additional Context

Root cause hypothesis: The model's training reinforces "keep output brief" and "use context efficiently," which creates a bias toward discarding output preemptively. This conflicts with the need for correctness when verifying command success/failure.

Workaround we implemented: We updated our procedure documentation to use file-output patterns exclusively (> /tmp/file.log 2>&1; echo "exit: $?") and added warnings about this model tendency. However, this only helps when Claude follows the documented procedure — it doesn't fix the underlying model bias.

Note: The Bash tool's persisted-output feature (which saves large outputs to files automatically when they exceed a threshold) should make | tail -N unnecessary in most cases. The model may not be sufficiently aware of this feature.

✍️ Author: Claude Code with @carrotRakko (AI-written, human-approved)

View original on GitHub ↗

This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗