Mahal Systems

Overview › Agent type

Code generation

Writing, editing, repairing software.

Specific to code generation

  1. 4.b.ii

    Initializer-maintainer dual agent

    Give the first session of a multi-session build a setup-only prompt (expand the spec into a JSON list of end-to-end tests, all failing, and write an init script, a progress file and a first commit) and every later session a maintainer prompt that reads that state, re-tests what passed, builds one failing item, verifies it end to end and commits.

    Memory › Resuming

    Better qualityBetter governanceMixed costSlower

    Tier 4 · restructuring · changes state · planning, code generation agents · 6 sources, 1 measured

  2. 9.c.i

    Don't prompt agents to write their own tests; suppress it for test-heavy models

    Agent-written tests are mostly print probes and do not change resolve rate.

    Verification › Sizing verification

    Lower costFaster

    Tier 3 · tuning · changes control flow · code generation agents · 6 sources, 2 measured

General optimizations with evidence from code generation

  1. 1.b.i

    Match the model and its effort to each call site and query class

    Pick the model per role or call site rather than one model everywhere, and set its reasoning effort and sampling parameters per site; tune each role's prompt to its model (GEPA raised every model tested at lower cost).

    Models › Finding specialists

    Better qualityLower cost

    Tier 3 · tuning · changes model, prompt · any agent · 19 sources, 6 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.ii

    Avoid few-shot ruts

    An agent's own history, and any examples in its prompt, act as few-shot demonstrations: when the context fills with near-identical action-observation pairs (twenty similar items in a row, a retried call), the model tends to copy their surface form instead of judging the current item.

    Prompts › Structuring

    Mixed qualityMixed cost

    Tier 3 · tuning · changes prompt · any agent · 13 sources, 8 measured

  4. 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

  5. 3.a.iv

    Restructure the system prompt: every requirement once, up front, in named sections; nothing the model doesn't need

    Rewrite the system prompt so that each requirement the task has (constraints, the answer format, the completion check, how to work efficiently, the domain procedure) is stated once, up front, in a named section, and remove instructions the model does not need.

    Prompts › Structuring

    Lower costFasterMixed quality

    Tier 3 · tuning · changes prompt · any agent · 11 sources, 2 measured

  6. 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

  7. 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

  8. 4.b.i

    Filesystem-based agent state and checkpoints

    Save the agent's state at each step boundary (the conversation, the workspace files, process state when it matters, and a ledger of external side effects) so a crashed or stuck run resumes or rolls back instead of starting over.

    Memory › Resuming

    Better qualityLower costMixed performance

    Tier 2 · reliability · changes state · any agent · 6 sources, 4 measured

  9. 5.a.i
    Context › Compacting

    Better qualityLower cost

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

  10. 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

  11. 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

  12. 6.a.i

    Oracle-worker multi-model

    Let a cheap model do the work and call an expensive one only when a trigger fires: a cascade escalates when the cheap answer looks unreliable or fails a check, a router picks the model before the call, and an advisor tool lets a cheap agent consult a stronger model mid-task.

    Topologies › Escalating

    Better governanceMixed qualityMixed costMixed performance

    Tier 4 · restructuring · changes model · any agent · 6 sources, 5 measured

  13. 6.b.i

    Parallel tool execution for read-only calls

    Let the model put several independent read-only tool calls (searches, fetches, file reads) in one turn and have the harness run them at once, while calls that write, share state or need another call's result run one at a time in the order the model emitted them.

    Topologies › Parallelizing

    Lower costFasterMixed quality

    Tier 3 · tuning · changes control flow · any agent · 6 sources, 3 measured

  14. 6.b.ii

    LLM map-reduce

    Give each piece of a large or untrusted input its own tool-less model call that sees only that piece and must answer in a fixed schema, then combine the answers in code or in a reducer that never sees the raw text.

    Topologies › Parallelizing

    More secureBetter governanceMixed qualityMixed costMixed performance

    Tier 2 · reliability · changes control flow · any agent · 6 sources, 4 measured

  15. 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

  16. 7.a.ii

    Prompt chaining with gate checks

    Split a task that decomposes cleanly into fixed steps into a chain of model calls, one goal per call, with a gate between steps: a schema check, an assertion, or a run against the real system, and an LLM check only for subjective criteria.

    Orchestration › Routing

    Better qualityMore secureBetter governanceBetter for brandHigher costSlower

    Tier 4 · restructuring · changes control flow · any agent · 11 sources, 8 measured

  17. 7.b.i

    Detect stalls and loops: warn, then stop early

    Track repeated identical actions (same tool, same arguments; same search query) and turns with no new information.

    Orchestration › Bounding execution

    Better qualityLower costFaster

    Tier 2 · reliability · changes control flow · any agent · 10 sources

  18. 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

  19. 7.c.i

    Reflection loop

    The same model critiques and revises its own output for a few rounds (Self-Refine), or after a failed attempt writes a short lesson it carries into the next try (Reflexion).

    Orchestration › Scaling compute

    Better for brandMixed qualityHigher costSlower

    Tier 3 · tuning · changes control flow · any agent · 11 sources, 10 measured

  20. 7.c.ii

    Self-critique evaluator loop

    Train or tune the system's own judge from its own outputs and synthetic judgments (contrastive pairs with one side deliberately degraded, the judge's own verdicts kept only where they match the construction label) instead of human labels, then use it to score, select or train in the loop.

    Orchestration › Scaling compute

    Mixed qualityHigher cost

    Tier 3 · tuning · changes control flow · any agent · 11 sources, 10 measured

  21. 7.c.iii

    Evaluator-optimizer loop

    A separate evaluator (an LLM judge with written criteria, a different model, or an agent that tests the running app) scores the generator's output and returns specific findings; the generator revises until the output passes or a round cap is hit.

    Orchestration › Scaling compute

    Better governanceBetter for brandMixed qualityHigher costSlower

    Tier 4 · restructuring · changes control flow · any agent · 15 sources, 13 measured

  22. 7.d.iii

    ReAct loop

    Run the agent as a loop: the model reasons, calls a tool, reads the result and picks the next step, until it answers or hits a cap.

    Orchestration › Shaping the loop

    Better qualityHigher costSlower

    Tier 4 · restructuring · changes control flow · any agent · 11 sources, 9 measured

  23. 9.a.i

    Feed back a sound external verifier's verdict; never the model's own critique

    Run a check the model cannot fool on its output (compiler, tests, schema validation, execution, EXPLAIN on generated SQL, a solver) and feed the verdict back; a pass/fail verdict is enough, and resampling until the check accepts works about as well as detailed feedback.

    Verification › Checking externally

    Better qualityHigher costSlower

    Tier 2 · reliability · changes control flow · any agent · 31 sources, 17 measured

  24. 9.b.iii

    Verify completion against the environment before the agent stops

    Never take the agent's own report that it finished.

    Verification › Confirming completion

    Better qualityHigher costSlower

    Tier 2 · reliability · changes control flow · any agent · 22 sources, 9 measured

  25. 10.a.i

    Constrain what the agent can touch

    Run the agent in an OS-level sandbox with a workspace path guard that resolves symlinks, scrub credentials from the command environment, run agent-written SQL read-only with timeouts and row caps, and set resource limits on generated code.

    Safety › Sandboxing

    Higher costSlower

    Tier 2 · reliability · changes guardrails · any agent · 5 sources

  26. 10.b.i

    Gate irreversible actions: allow, ask or deny by side effect

    Classify every action the agent can take by its side effect (read, reversible write, irreversible or externally visible: sending email, issuing a refund, deleting a record, posting publicly, pushing code) and gate by class: allow, ask a human or a policy, or deny.

    Safety › Gating risky actions

    Slower

    Tier 2 · reliability · changes guardrails · any agent · 9 sources

  27. 10.c.i

    Contain sensitive data and untrusted input

    Tokenize PII in the client so it never reaches the model, wrap untrusted tool and web results in taint delimiters, scan context files and skills for prompt injection, and state an explicit instruction-precedence order (user, repo, defaults, external data as data).

    Safety › Containing data

    Tier 2 · reliability · changes guardrails · any agent · 4 sources

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.