# Budget

A coding agent reads a command's output through a host that cuts long output. readerly gives `agent` output a character budget, keeps whole lines that fit, and tells the agent what it left out.

## The default is 8,000 characters

`DEFAULT_REPORT_BUDGET` in [`src/budget.ts`](https://github.com/loadsheet/readerly/blob/main/src/budget.ts) is `8000`. That is under Claude Code's 30,000-character Bash output limit and Codex's 10,000-token cap. Codex cuts from the middle, so a report that runs over loses the lines an agent needs most, between the verdict and the next steps.

The profile sets the budget in this order:

| Case | `profile.budget` | `profile.why.budget` |
| --- | --- | --- |
| `--full` | `Infinity` | `--full` |
| `--max-chars <n>` | `n` | `--max-chars` |
| format `agent` | `8000` | `format agent` |
| an agent reader on `text` | `8000` | `agent reader on text` |
| anything else | `Infinity` | `no budget for <reader> on <format>` |

Only the `agent` renderer reads it. `text`, `json` and `jsonl` print every line whatever the budget says. A [custom renderer](https://readerly.loadsheet.dev/guide/renderers) can read `ctx.profile.budget` and decide for itself.

## What the agent keeps

The `agent` renderer plans its lines before it prints them:

- **Whole lines.** A diagnostic is kept or dropped as one line. readerly never cuts a line in half.
- **Verdict first.** Line 1 is always the verdict, with the status and the full counts, even when most rows are hidden.
- **Next steps before rows.** `next:` lines are planned first, so a suggested command survives a tight budget.
- **Errors before warnings.** Then errors, warnings, infos and logs, in that order. No warning gets in while an error is still left out.
- **One of each code.** Inside a severity, rows group by code. Every code gets its first row before any code gets a second, so 120 copies of one warning can't push out a different one.

The first line that doesn't fit stops the plan. Everything after it is hidden, and the footer counts it.

## The hidden footer

To see the footer, run a lint with 3 errors and 120 warnings as a coding agent. The output is 7,963 characters: the verdict, 3 errors, 91 warnings, the next step and this last line:

```text
warn ACME010 packages/pkg-91/vite.config.ts:8:5 Alias shadows a workspace package
next: acme lint --fix   (applies 3 safe fixes)
hidden: 29 warn. rerun: acme lint --full, or acme lint --format json
```

The footer names each kind it dropped (`error`, `warn`, `info`, `log`, or `lines` of an [answer](https://readerly.loadsheet.dev/guide/renderers#answers-the-tool-renders)) and two commands that get the rest: the same invocation with `--full`, and with `--format json`. readerly reserves room for the longest footer it could print before it plans the final cut, so the footer never pushes the total over the budget, unless the budget is smaller than the verdict and the footer together.

## --max-chars and --full

`--max-chars <n>` sets the budget to any positive integer. The same lint with `--max-chars 600` gives a coding agent 511 characters:

```text
acme lint: failed. 3 errors, 120 warnings.
error ACME001 src/routes/r1.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
error ACME001 src/routes/r2.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
error ACME001 src/routes/r3.ts:14:3 Route handler reads process.env at module scope. fix: Read it inside the handler
next: acme lint --fix   (applies 3 safe fixes)
hidden: 120 warn. rerun: acme lint --full, or acme lint --format json
```

`--full` removes the budget and adds `why:` and `docs:` to each line. The same run prints 10,293 characters with no footer.

A value that isn't a positive integer is a usage failure with exit code 2. A coding agent gets:

```text
acme build: failed. --max-chars abc is not a positive integer.
fix: Pass --max-chars with a positive integer, such as --max-chars 8000.
```

## Failures under a budget

A failure report is a few lines: the verdict, `fix:`, any `missing:` inputs, `rerun:`, and with `--full`, `why:` and `docs:`. When those run over the budget, readerly drops `docs`, then `why`, then `fix`, and adds `hidden: fix. rerun: <command> --full`. The verdict, the missing inputs and the rerun line always stay, because the agent needs them to act. See [Failures and exit codes](https://readerly.loadsheet.dev/guide/failures).

## Using planBudget on its own

`planBudget` from `readerly/budget` is the planner the `agent` renderer uses, without any text. You give it sizes in characters, and it returns the units that fit.

```ts [plan.ts]
import { hasHidden, hiddenCounts, planBudget, shownGroups, type BudgetGroup } from "readerly/budget";

const groups: BudgetGroup[] = [
  { tier: 1, header: 40, rows: [60, 60, 60], detail: 120 },
  { tier: 2, header: 40, rows: [50, 50], detail: 80 },
];
const units = planBudget(20, groups, 300);
const shown = shownGroups(groups, units);
const hidden = hiddenCounts(groups, shown);
console.log(JSON.stringify(shown));
console.log(JSON.stringify(hidden), hasHidden(hidden));
```

Each `BudgetGroup` is one group of rows, such as every finding of one code. `tier` orders groups, lower first. `header` is the group's heading at its longest, `rows` the size of each row, and `detail` what showing every row in detail would add. `fixed` is what the head and footer cost, here 20.

Here the tier 1 group fits with all three rows, 240 characters with `fixed`. The tier 2 header and its first row would take 90 more and pass 300, so the plan stops. The script prints:

```text
[{"header":true,"rows":3,"detail":false},{"header":false,"rows":0,"detail":false}]
{"groups":1,"rows":2,"detail":5} true
```

Detail is added only when nothing else is hidden, one whole group at a time. `hasHidden(hidden)` is `true` when any group, row or detail is left out.

Next: [Prompts and input](https://readerly.loadsheet.dev/guide/prompts).

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://readerly.loadsheet.dev/raw/guide.md)
- [Installation](https://readerly.loadsheet.dev/raw/guide/installation.md)
- [Readers](https://readerly.loadsheet.dev/raw/guide/readers.md)
- [Coding agents](https://readerly.loadsheet.dev/raw/guide/agents.md)
- [Formats](https://readerly.loadsheet.dev/raw/guide/formats.md)
- [Shared flags](https://readerly.loadsheet.dev/raw/guide/flags.md)
- [Color and glyphs](https://readerly.loadsheet.dev/raw/guide/color.md)
- [Diagnostics](https://readerly.loadsheet.dev/raw/guide/diagnostics.md)
- [Budget](https://readerly.loadsheet.dev/raw/guide/budget.md) (this page)
- [Prompts and input](https://readerly.loadsheet.dev/raw/guide/prompts.md)
- [Failures and exit codes](https://readerly.loadsheet.dev/raw/guide/failures.md)
- [JSON and JSONL](https://readerly.loadsheet.dev/raw/guide/json.md)
- [Custom renderers](https://readerly.loadsheet.dev/raw/guide/renderers.md)
- [API](https://readerly.loadsheet.dev/raw/guide/api.md)

Next: [Prompts and input](https://readerly.loadsheet.dev/raw/guide/prompts.md)

All pages: [llms.txt](https://readerly.loadsheet.dev/llms.txt)
