feat(agents): add per-agent maxContextBytes

Merged
OK
3d ago
+109-26

What

An agent takes maxContextBytes, the bytes its context may take: 1024 through 65536, default 16384.

agents: {
  default: {
    context: longText,
    maxContextBytes: 65_536,
    maxInputTokens: 128_000,
  },
},

A context that leaves the screen no room under the agent's maxInputTokens now fails the config load with INVALID_CONFIG.

Why

#646 removed the top-level limits key and fixed the context limit at 16384 bytes. A customer with a longer app vocabulary has no way to raise it.

How it works

  1. resolveAgentConfig reads maxContextBytes per agent, with the range the report schema already allows for limits.maxAgentContextBytes (packages/core/src/config/agent.ts:214).
  2. validateContext checks the context against the agent's own limit. The error names maxContextBytes unless the agent is already at 65536 (packages/core/src/config/agent.ts:479).
  3. validateContext also rejects a context whose bytes, plus the instruction reserve and the smallest screen the runner sends, exceed maxInputTokens (packages/core/src/config/agent.ts:490). A request is counted in bytes, so with the default 64000 a 60 KB context used to load and then fail every model call with STEP_BUDGET_EXHAUSTED. The fixed 16384 could never reach that; the raised limit can.
  4. runLimits reports the largest maxContextBytes of any agent as run.limits.maxAgentContextBytes, as it does for the other per-agent limits (packages/core/src/config/agent.ts:176).
  5. The removed limits key and an agent-level maxAgentContextBytes point at maxContextBytes (packages/core/src/config/resolve.ts:186, packages/core/src/config/agent.ts:120).

Not changed: a test's or group's agentContext has no size check, on main or here. The Run limits table in the config reference said it had one, and no longer does.

Verified

Ran it locally: yes

Built CLI on the testbed, e2e list --config <scratch> with a scratch config that spreads the testbed's and sets agents.default.context:

== 40800-byte context, no maxContextBytes
INVALID_CONFIG: agents.default.context is 40800 bytes; the maximum is 16384, raise it with agents.default.maxContextBytes (up to 65536)
== maxContextBytes: 32768
INVALID_CONFIG: agents.default.context is 40800 bytes; the maximum is 32768, raise it with agents.default.maxContextBytes (up to 65536)
== maxContextBytes: 65536
(lists the tests)
== maxContextBytes: 70000
INVALID_CONFIG: agents.default.maxContextBytes must be an integer from 1024 through 65536
== 62400-byte context, maxContextBytes: 65536
INVALID_CONFIG: agents.default.context is 62400 bytes, which leaves no room for the screen under agents.default.maxInputTokens 64000 (a request is counted in bytes); set agents.default.maxInputTokens to at least 67520, more for dense screens
== 62400-byte context, maxContextBytes: 65536, maxInputTokens: 128000
(lists the tests)
== 67200-byte context, maxContextBytes: 65536, maxInputTokens: 128000
INVALID_CONFIG: agents.default.context is 67200 bytes; the maximum is 65536

e2e run tests/board.e2e.ts --grep "hover reveals" with maxContextBytes: 65536 passes, and its report.json has run.limits.maxAgentContextBytes: 65536.

No model call ran with a long context: I had no model key in this checkout. The docs pages have no screenshot because the docs site does not build locally.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only.
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md).
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it.
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures.
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile.