ALPHAMeet your codebase’s new constraint

Complexity budgets for coding agents

Grow the product.
Prune the code.

Your coding agent can build almost anything.
Bonsai gives it a reason to build less.

A small tool. An explicit stack.

TSTypeScript◇Gherkin contracts◒Bun runtime{ }JSON for agents
THE MECHANISM, MADE VISIBLE

A contract. A ceiling. A reason to prune.

bonsai/checkout
1 / 3 · Check
TSsrc/checkout.tsThe candidate
1export function validQuantity(quantity: number) {
2 return Number.isInteger(quantity)
3 && quantity > 0
4}
5
6export function placeOrder(input: { quantity: number }) {
7 return { status:
8 validQuantity(input.quantity)
9 ? 'accepted' : 'rejected' }
10}
Both functions trace to the checkout contract.

Change this file.Watch the verdict respond.

›_bonsai checkThe verdict
✓

Room to grow.

Required behavior. Justified code. Within budget.

Pass
Retained source
212 / 400 tokens
Contract surface
1 pair
Required checks
✓ 3 / 3 passed
Untraced components
0
Retained codeGlobal ceiling

Passing tests are one condition. Staying lean is another.

A closer look at the checkout example. Explore the complete project

Deliberate growth. Continuous pruning.

Why Bonsai exists

Agents write fast.
Codebases get heavy.

AI agents add code in seconds. The extra layers slow every next feature.
Bonsai keeps your agent pruning before the codebase gets out of hand.

Keep the codebase growing, without the buildup A bonsai grows branches of code. Pruning shears cut an unnecessary pink offshoot while the main blue, lavender, and green branches remain.
Keep the behavior. Prune the buildup.

Passing tests are only the beginning

The code has to
earn its place.

Bonsai checks required behavior, contract ancestry,
and retained size together.

Behavior earns room.

Executable product contracts define the external surface. Distinct action–observation pairs determine one global allowance.

See the contract ↗

Every component has a root.

Retained components need a path to a product contract. Lower-level tests inherit that ancestry; they cannot invent it.

Understand ancestry ↗

Simplify to grow.

When the next feature exceeds the ceiling, reclaim room. Inline the wrapper. Remove the duplication. Keep the required behavior.

Explore the budget ↗

Describe behavior once

More checks.
Same allowance.

Accepted orders. Invalid quantities. Boundary cases. Each example must pass, but repeating an action and result never buys more code.

The specification stays in .feature files. A small executable registry connects it to your real handlers.

3✓required checks
1action–observation pair
◇features/checkout.feature
Feature: Checkout
  Scenario Outline: Place an order
    When I invoke "place-order" with {"quantity": <quantity>}
    Then response "status" equals "<status>"

    Examples:
      | quantity | status   |
      | 1        | accepted |
      | 0        | rejected |
      | -1       | rejected |

Diminishing room. Increasing discipline.

Growth gets room.
Then it gets pressure.

Start with room to bootstrap. After that, each doubling of external contract surface adds a fixed amount of capacity. Choose the bootstrap allowance, growth rate, and absolute ceiling.

More capability has to fit an increasingly economical implementation.

See the policy
↳

A shared wrapper still pays its full cost.
Reuse earns room when it removes real duplication.

Where the code meets the ceiling.

Contract allowance
20,000tokens
Retained source
24,000tokens
Retained source against the contract-derived allowanceThe purple allowance curve rises from 20,000 tokens at 20 contract pairs to 26,000 at 160. The dashed retained-source line is independently adjustable. A 24,000-token candidate meets the allowance at 80 pairs.28k24k20k16k204080160

Contract surface · pairs

4,000 tokens to prune.

20 pairs
24,000 tokens

Beyond a green test suite

The difference is
what gets to stay.

BDD checks what must work. Bonsai also limits what gets to stay.

Behavior contracts, with compaction.BonsaiCOMPACTIONBDD runnerCucumber aloneLintersalone
Executable .feature scenarios✓✓−
Checks expected scenario outcomes✓✓−
Requires contract ancestry✓−−
Caps total maintained source✓−−
Reports required budget reduction✓−−
Enforces lint rules−−✓

Bonsai builds on executable behavior contracts. It adds retention rules and a code budget.
Comparison reflects standalone use; custom integrations can add capabilities.

Your agent. A tighter loop.

The next feature may
start with a deletion.

Bonsai supplies the facts and the limit.
Your coding agent does the reasoning and the pruning.

Measure the room.

Start with the retained size, required checks, and available headroom.

bonsai check

Follow the roots.

See why a component belongs and which relationships keep it connected.

bonsai explain src/checkout.ts

Prune the excess.

Inline the wrapper. Remove the duplication. Keep the required behavior.

Keep the promises.

Run a fresh check. Preserve the behavior and prove the candidate fits.

bonsai check

Get started

Set up Bonsai.
Your way.

Choose one route: ask your coding agent to set it up, or configure the project yourself.

Agent-assisted setup

Give this to
your coding agent.

Paste the prompt into the coding agent you already use. It guides the skill installation, policy, and first real contracts.

Get the agent skill
Paste into your coding agent
1. Install the complete Bonsai skill from https://github.com/jiggy/bonsai/tree/main/skills/bonsai in this project's skills directory, including `references/`. Read `SKILL.md`.

2. Follow `references/adoption.md`: initialize Bonsai, review the inventory and policy in `bonsai.json`, and connect real handlers to `.feature` contracts. Preserve required behavior and existing tests.

3. Verify a passing baseline, then demonstrate unused-code rejection and recovery. Add the project gate command and agent instructions.

A few good questions

Small by design.
Explicit about the limits.

Read the roadmap
Does every helper need its own acceptance test?+

No. A helper can inherit contract ancestry through the production component that needs it. Lower-level tests then inherit from retained subjects. A unit test cannot justify otherwise unsupported production code.

Can an agent buy more room with more tests?+

Extra examples, duplicate scenarios, and split files earn no additional allowance. Each required check still has to pass. Only distinct external action–observation pairs affect the surface count.

Is this just a line-count limit?+

Bonsai measures retained source tokens within an explicit maintained inventory, including contracts, bindings, tests, fixtures, and configuration. Formatting outside TypeScript literals creates no headroom. The metric creates consistent pressure; it does not claim to measure comprehension or predict effort.

Does Bonsai delete code automatically?+

Bonsai reports the constraint and blocks acceptance. Your existing coding agent proposes and verifies the change. Unsupported code must be removed, connected to required behavior, or recovered through a legitimate contract correction.

What can I use today?+

Bonsai supports TypeScript and named in-process handlers, a small executable Gherkin grammar, static symbol ancestry, retained-size budgets, and candidate-bound contract results. Use it for a supported pilot and review the maintained inventory.

HTTP and queue adapters, coverage and mutation evidence, and hardened merge enforcement are on the roadmap. Contract handlers execute with your privileges; use an external sandbox for untrusted candidates. Read the full limits ↗

Deliberate growth. Continuous pruning.

Build what matters.
Keep only what earns its place.