The loop
Context in, structured next step, execute, append, repeat. Own this even if a framework wraps it.
python
context = [event]
budget = Budget(max_steps=20, max_tokens=80_000, max_usd=2.0)
while True:
budget.check(context)
nxt = llm.determine_next_step(
context,
tools=allowed_tools(state),
schema=NextStep, # includes intent: tool | done | ask_human
)
if nxt.intent == "done":
return nxt
if nxt.intent == "ask_human":
save_checkpoint(state, context)
return await pause_for_human(nxt)
result = execute(nxt) # allow / ask / deny lives here
context.append(compact(result))- Termination, step/token/money budgets, and pause/resume are first-class.
- execute() is the sandbox and the permission gate.
- TypeScript cousin: the same shape with a JSON schema on next_step.