Skip to content

Token-budget regressions: image marker cost dropped, fixed trim reserve guts small local models, silent trim #4462

Description

@senamakel

Part of the v0.58.7 → HEAD TinyAgents-migration audit (see parent issue). Replaces deleted harness/token_budget.rs (696 lines) semantics that regressed in the crate trim path (TA summarization/mod.rs:43, MessageTrimMiddleware installed at src/openhuman/tinyagents/mod.rs:1330).

Regressions

  1. Image marker token cost dropped. Legacy priced image markers at IMAGE_MARKER_TOKEN_COST = 1200 tokens. The crate estimates chars/4, so a large base64 image counts as ~2M tokens → the trim believes the context is massively over budget and can drop everything, including system messages. (The crate trim also reorders system messages to front and is allowed to drop them — both legacy regression guards lost.)
  2. Trim reserve formula regressed. Legacy reserve: clamp(window/10, ≥512, ≤max(8192, window/4)) — proportional. New: fixed window − 16384 floored at 1024. An 8k local model's input budget collapses from ~7373 to 1024 tokens, gutting small local-model contexts.
  3. Trim is silent. Legacy warned with counts; the crate trims with no log/outcome, so context loss is undiagnosable.

Fix plan

  1. Pre-count multimodal content before the trim sees it: substitute image/file markers with their fixed token cost in the estimator input (adapter-side: wrap or pre-process messages fed to MessageTrimMiddleware, or supply a custom TokenEstimator if the crate accepts one; if not, upstream that hook).
  2. Restore the proportional reserve for small windows: compute the trim target ourselves (legacy clamp formula) and configure the middleware with it, instead of the fixed window − 16384.
  3. Guarantee system messages survive: assert/keep the system prefix out of the droppable set at the adapter layer; add a regression test with an oversized image + small window asserting the system message survives.
  4. Log a warn with (messages dropped, tokens before/after) whenever the trim removes anything; consider emitting a DomainEvent for the UI.

Acceptance criteria

  • One large image no longer evicts the system prompt or the rest of the transcript.
  • On an 8k-window local model, input budget matches the legacy proportional formula (±rounding).
  • Any trim produces a grep-able warn with counts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentBuilt-in agents, prompts, orchestration, and agent runtime in src/openhuman/agent/.buglocal-aiLocal model runtime, Ollama integration, and local AI config.priority: highBreaks a specific featuresubtaskSubtask of a larger tracked effort.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions