AGENTICAMIT // LOOP ENGINEERING an engraving of an agent

AgenticAmit // sheet 1 of 1 // section view

Six decisions in a circle.

Every AI agent is the same machine: a loop. The model is one stop on the ring. Scroll to walk it. At the end, it runs.

Stage 01// ingest

It only knows what you hand it.

Somebody's code decides what the agent sees: which files, which memories, which tools. Choose badly and no model can save you.

def ingest(state) -> Context:
    files  = workspace.relevant(state.task)
    memory = store.recall(state.task)
    tools  = registry.available()
    return Context(files, memory, tools,
        history=state.history[-KEEP:])

// most "model failures" are ingest failures: the fact was never in the window.

Stage 02// assemble

Order is a decision.

Same facts, different order, different answer. Rules go first, the ask goes last: recency is weight.

def assemble(ctx) -> Prompt:
    return Prompt(
        system    = RULES + ctx.tools.schemas(),
        history   = ctx.history,
        knowledge = ctx.files + ctx.memory,
        ask       = ctx.task,  # last: recency wins
    )

// prompt = f(context). Can't diff it, can't debug it.

Stage 03// model call

The famous part is one line.

Tokens in, tokens out. You control everything on either side of this call, and nothing inside it. That's why the rest of the ring exists.

def model(prompt) -> Reply:
    # the one line you rent, not own
    return llm.generate(
        prompt,
        temperature=0.2,
        stop=STOP_TOKENS,
    )

// swap the model; this page barely changes.

Stage 04// parse & route

Words in, actions out.

The model can only reply with text. Parsing turns text into action: say this, call that.

def parse(reply) -> list[Action]:
    acts = []
    for block in reply.blocks:
        if block.kind == "tool_call":
            acts.append(Call(block.name, block.args))
        else:
            acts.append(Say(block.text))
    return acts

// malformed tool calls go back around as errors, not crashes.

Stage 05// execute + observe

Do it where it can't hurt.

Every tool call runs inside a wall. What comes back is an observation the agent has to read like anything else.

def execute(acts) -> list[Result]:
    results = []
    for act in acts:
        out = sandbox.run(act)  # never raw
        results.append(observe(out))
    return results

// the observation is the product of a tool call, not its side effect.

Stage 06// compact + verify

Forget on purpose.

Keeping everything is a way of forgetting everything. Summarize what happened, check the work, then go around.

def compact(state, results) -> State:
    state.history += results
    if state.tokens() > BUDGET:
        state.history = summarize(state.history)
    state.todos = verify(state, results)
    return state

// compaction is lossy on purpose. What survives is a design decision.

The gate// continue or exit

The only question that matters.

Every lap ends at one if-statement. Checks green: return. Anything left: around again. Going around again is the feature.

# loop.py: the whole agent
def run(task) -> Answer:
    state = State(task)
    while True:          # the ring you just walked
        state = turn(state)
        if state.done():  # the gate
            return state.answer

// exit on verified todos, not on the model feeling done.

That's one turn.

now give it a real task

Live// task: fix the flaky retry test

Now watch it compound.

Every lap starts with what the last one learned. The trail is the record.

  • reproduce the failureturn 02
  • locate the causeturn 03
  • write the patchturn 04
  • re-run the suiteturn 04
  • write it upturn 05

// verify caught its own bad patch on turn 04. The loop is the safety net.

Return// gate open5 turns · ctx 42k

It earned the exit.

Five turns, one failure, caught and fixed by the loop itself. The answer leaves the only way it can: through the gate.

The drawing, complete

The loop is the product.

The model was one stop: the only line you don't write. Every other stop is a design decision. That is loop engineering.

Loop Engineering · an AgenticAmit engraving.