Mahal Systems

Overview › Lever

Context

What enters and stays in the window: compaction, filtering, isolation, cache layout. 12 optimizations change this part of an agent.

  1. 2.a.i
    Tools › Refactoring

    Better qualityLower costMixed performance

    Tier 1 · hygiene · changes tool surface, context · tool use agents · 32 sources, 23 measured

  2. 2.c.i

    Bound tool output: cap it, or write it to a file the agent can read

    Cap the characters of tool output kept per call (VeRO settled near 15-20 KB on TerminalBench), or, better, write long output to a file and return its path with a short head, so the agent can read more if it needs to.

    Tools › Trimming results

    Lower costFasterMixed quality

    Tier 3 · tuning · changes tool surface, context · any agent · 7 sources

  3. 3.a.iii

    Dynamic context injection

    Put the runtime facts the model cannot know into its context each run: today's date and timezone, the working directory, OS and git state, the tools available, the user and locale.

    Prompts › Structuring

    Better for brandMixed quality

    Tier 3 · tuning · changes prompt, context · any agent · 9 sources, 4 measured

  4. 3.b.i

    Keep the prompt prefix cache-stable

    Put static content (identity, rules, tool definitions) before a boundary and per-turn content (date, state, memory) after it; keep the prefix bit-for-bit identical across turns; strip thinking text from persisted history; and track per-turn cache-miss cost.

    Prompts › Caching

    Lower costFaster

    Tier 1 · hygiene · changes context · any agent · 8 sources, 1 measured

  5. 3.b.ii

    Mask tools instead of removing them

    When the tools an agent may use change mid-task (by mode, phase or permission), declare every tool once, keep the definitions byte-stable, and restrict the choice another way: OpenAI's allowed_tools or tool_choice "none", Anthropic's appended tool_removal and tool_addition blocks, a decode-time logit mask or grammar on self-hosted models, and a harness check that rejects calls outside the allowed set.

    Prompts › Caching

    Lower costFasterMore secureBetter governanceMixed quality

    Tier 3 · tuning · changes context, tool surface · any agent · 11 sources, 5 measured

  6. 5.a.i
    Context › Compacting

    Better qualityLower cost

    Tier 2 · reliability · changes context · any agent · 19 sources, 5 measured

  7. 5.b.i

    Give the model the schema it queries, in context: descriptions, keys, codes, sample values

    When the model writes SQL or structured filters, put the schema it is querying in its context: table and column descriptions, key relationships, what enum codes and nulls mean, sample values.

    Context › Filtering input

    Better qualityHigher cost

    Tier 3 · tuning · changes knowledge access, context · data analysis agents · 4 sources, 2 measured

  8. 5.c.i

    Discrete phase separation

    Run research, planning and implementation as separate agent sessions, each starting from a fresh context that holds only the previous phase's written output (a research document, then a plan file), and have a person review that document before the next phase starts.

    Context › Isolating subtasks

    Mixed qualityMixed costSlower

    Tier 4 · restructuring · changes context · any agent · 6 sources, 3 measured

  9. 5.c.ii

    Sub-agent architectures with clean contexts

    Hand a context-heavy side task (code search, reading many files, documents or logs) to a sub-agent that starts with a fresh context and only the tools it needs, and have it return a short result or file references so the parent's context stays small.

    Context › Isolating subtasks

    Mixed qualityMixed costMixed performance

    Tier 4 · restructuring · changes context · any agent · 12 sources, 10 measured

  10. 6.b.iii

    Use multi-agent orchestration only for breadth-first, independent sub-tasks

    Spawn parallel sub-agents when the work splits into independent, breadth-first parts with little shared context; expect about 15x the tokens of a chat and up to 90% less wall-clock.

    Topologies › Parallelizing

    FasterMixed qualityHigher cost

    Tier 4 · restructuring · changes control flow, context · any agent · 22 sources, 7 measured

  11. 7.b.ii

    Keep errors in context

    Return every failed action to the model as an error it can read (the call, the cause, a capped trace) instead of swallowing the exception, retrying silently, returning an empty result or deleting the failed attempt, and collapse each error once it is resolved.

    Orchestration › Bounding execution

    Better governanceMixed qualityMixed cost

    Tier 2 · reliability · changes control flow, context · any agent · 11 sources, 7 measured

  12. 7.b.iv

    Track explicit progress: a task list with a status per item, re-injected each turn

    Give the agent a task-list tool (each item with a status, one in progress at a time), ask for the list on the first turn of non-trivial tasks, and re-inject the current list after the system prompt each turn instead of keeping old copies in history.

    Orchestration › Bounding execution

    Mixed qualityMixed costMixed performance

    Tier 3 · tuning · changes control flow, context · planning agents · 1 source, 1 measured

Before you go...

Build better agents, weekly

Our latest research, performance benchmarks, optimization strategies, and updates around AOT development.

One email a week, nothing else. Unsubscribe anytime.