# ANSI colour codes without a terminal

> ax-check rule: Output contained ANSI escape sequences although stdout and stderr were not terminals.

Section: ax-check rule · CLI behaviour  
Canonical: https://agentexperience.tech/checks/axc-c010/

Output contained ANSI escape sequences although stdout and stderr were not terminals.

- **How serious**: **warn** A gap that leaves the agent guessing.
- **Kind of check**: **[Observed](https://agentexperience.tech/checks/#kinds)** Depends on the program and environment at run time.
- **ax-check command**: `ax-check probe-cli`
- **Where it applies**: Command-line programs, run as a subprocess
- **Topics**: [non-interactive](https://agentexperience.tech/evidence/#pattern-non-interactive), [context-budget](https://agentexperience.tech/evidence/#pattern-context-budget)
- **Rule text from**: ax-check 0.2.0Documented ahead of release

**Fix in one line** Only colour output when the stream is a TTY; check each stream separately.

The output contained ANSI escape sequences even though stdout and stderr were pipes, not terminals. Those sequences are invisible on a screen but show up as noise when a program or an agent reads the raw bytes.

## What it checks

The rule scans the output of every probe that ran, except the no-color probe, for ANSI escape sequences, such as `ESC[31m` for colour or `ESC[2K` for erasing a line. It produces one finding, which lists every probe that emitted one. Every probe runs with no TTY, with `CI=1` set, and with `FORCE_COLOR`, `CLICOLOR_FORCE` and `NO_COLOR` removed from the environment. This means ax-check has not asked for colour in any way.

## Why it matters

Raw escape codes break string matching, add characters to every line and make parsed output harder to read. An agent that reads `\x1b[31merror\x1b[0m` must work out that the extra characters are decoration. They also take space in the model context for no gain. The Command Line Interface Guidelines say to disable colour when “stdout or stderr is not an interactive terminal (a TTY)”, when `NO_COLOR` is set, when `TERM` is `dumb`, or with `--no-color`.

## How to fix

Colour only when the stream is a TTY, and check stdout and stderr separately, since one can be a terminal while the other is a pipe. Most colour libraries do this for you, but only if you ask them to detect the stream and not force colour on.

## Example

### Before

```console
$ invoicer list --status bogus </dev/null 2>&1 | cat -v
^[[31merror^[[0m: unknown status "bogus"
^[[2mValid values: open, paid, void^[[0m
```

### After

```console
$ invoicer list --status bogus </dev/null 2>&1 | cat -v
error: unknown status "bogus"
Valid values: open, paid, void
```

A Node fix:

```js
const useColour = process.stderr.isTTY && !process.env.NO_COLOR && process.env.TERM !== 'dumb';
const red = (s) => (useColour ? `\x1b[31m${s}\x1b[0m` : s);
console.error(`${red('error')}: unknown status "bogus"`);
```

A Python fix:

```python
use_colour = sys.stderr.isatty() and not os.environ.get("NO_COLOR") and os.environ.get("TERM") != "dumb"
```

## How ax-check detects it

ax-check searches the captured stdout and stderr of each probe for any ANSI escape sequence: `ESC [`, then parameters, then a final character. It does not decode what the sequence does, so cursor movement and line erasing count as well as colour. That is deliberate: none of these belong in piped output. [NO\_COLOR not honoured](https://agentexperience.tech/checks/axc-c011/) is narrower and checks colour codes only.

The no-color probe is not scanned here, because [NO\_COLOR not honoured](https://agentexperience.tech/checks/axc-c011/) covers it. If your program colours only when `FORCE_COLOR` is set, it passes here because ax-check removes that variable.

To reproduce by hand: `invoicer --help </dev/null 2>&1 | cat -v | grep -n '\^\[\['`.

Known false positives: tools whose purpose is to produce coloured output for later display. Silence the rule with `--disable <rule-id>` for those.

Safety note: This rule scans every probe that ran; by default, only the help and version probes. probe-cli executes your program under your own OS account. The temporary working directory and HOME it uses are not a sandbox: the program can still read and write anything your account can, and use the network. Probe a CLI you have not reviewed only inside a container or a throwaway virtual machine. Run `ax-check probe-cli --dry-run -- invoicer` first to see the probes that would run, and see the “Probe safety” section of the ax-check README for what is and is not isolated.

## Sources

- Guideline: Command Line Interface Guidelines, clig.dev. [https://clig.dev/](https://clig.dev/) . Lists the cases where colour should be disabled: stdout or stderr is not a TTY, `NO_COLOR` is set, `TERM` is `dumb`, or `--no-color` is passed.
- Specification: NO\_COLOR, no-color.org. [https://no-color.org/](https://no-color.org/) . Says software that adds ANSI colour by default should check for `NO_COLOR`.

## Related evidence

Records in the [AX evidence register](https://agentexperience.tech/evidence/) about the same pattern as this rule. They don’t test the rule itself.

- [Injected skills lowered pass rates and raised token cost on average](https://agentexperience.tech/evidence/ev-0017/): WebDev-Skills-Bench (preprint) found that injecting matched public skills reduced mean Pass@2 by 1.3% to 4.2% across four models and raised token cost by 72% to 394%, with gains in only 17% to 36% of skill-project pairs. Preprint: not yet peer reviewed
- [Search-and-execute meta-tools cut catalogue tokens by 99% in production](https://agentexperience.tech/evidence/ev-0022/): PayPal authors report (preprint) that exposing two meta-tools, search and execute, over 2,000+ MCP tools cut tool-token consumption in production from 140.2k tokens (70.1% of context) to 1.3k tokens (0.8%). Preprint: not yet peer reviewed
- [A capable agent skipped the index and guessed the page](https://agentexperience.tech/evidence/ev-0027/): A preregistered ablation on a 709-page Markdown wiki (preprint) found that a capable tool-using agent never loaded the compact catalogue index, inferring page paths from the question instead, while retrieval-based access kept answer quality non-inferior and cut cost by about a third to over half. Preprint: not yet peer reviewed
- [A CLI exits 0 when a destructive command is refused](https://agentexperience.tech/evidence/ev-0036/): Cloudflare's cf CLI documents that in a non-interactive session a destructive command without --force prints 'Aborted.' and exits with status 0, and a public issue reproduces this for workflows delete. Independent measurement: no stake in the product
- [Launch post and documentation disagree on agent output format](https://agentexperience.tech/evidence/ev-0039/): Cloudflare's cf launch post says JSON output is 'condensed for agents', but the cf documentation says JSON output is indented whether or not output is a terminal, and a public issue reports byte-identical output with an agent detected. Independent measurement: no stake in the product

## Related guides

- [Agent error recovery: design the path back](https://agentexperience.tech/insights/design-for-recovery/): Agent error recovery in practice: what a failure response must say, when a retry is safe, how to handle partial completion, and when to escalate. Guide
- [Agent tool catalogues need a pruning strategy](https://agentexperience.tech/insights/agent-tool-catalogs/): How to design an agent tool catalogue: why selection gets harder as an MCP tool catalog grows, three ways to prune it, and how to name neighbouring tools. Guide

## Go here next

- [Agent error recovery: design the path back](https://agentexperience.tech/insights/design-for-recovery/): Four fields turn an error into a decision: what failed, whether state changed, whether a retry is safe, and what input would fix it. Guide
- [A CLI exits 0 when a destructive command is refused](https://agentexperience.tech/evidence/ev-0036/): Cloudflare's cf CLI documents that in a non-interactive session a destructive command without --force prints 'Aborted.' and exits with status 0, and a public issue reproduces this for workflows delete. Evidence
- [NO_COLOR not honoured](https://agentexperience.tech/checks/axc-c011/): With NO_COLOR=1 set, output still contained ANSI colour codes. Rule

## About this rule text

[Help is not reachable](https://agentexperience.tech/checks/axc-c009/)

[NO\_COLOR not honoured](https://agentexperience.tech/checks/axc-c011/)

[All ax-check rules](https://agentexperience.tech/checks/)

Rule text from ax-check 0.2.0, by Himadri Mishra. The licence will be confirmed before the source repository is published.
