One conversation of a code-writing model: it writes a block, the sandbox runs it, the feedback goes back.
Shared by the code.llm agent (one episode) and research sessions (many episodes). Accounting per call (model,
tokens, latency, cost) and running totals; every turn becomes one record handed to emit (the agent writes it to
the trace as agent.code.turn, a research session to its log).
Budget
Bases: Contract
turns
class-attribute
instance-attribute
turns: int = Field(default=30, ge=1)
tokens
class-attribute
instance-attribute
tokens: int | None = None
cost_usd
class-attribute
instance-attribute
cost_usd: float | None = None
steps
class-attribute
instance-attribute
resets
class-attribute
instance-attribute
resets: int | None = None
submits
class-attribute
instance-attribute
submits: int | None = None
parse
classmethod
parse(text: str | Mapping[str, Any] | None) -> 'Budget'
From a mapping or "turns=40,cost=2.5,submits=5" ("cost" and "cost_usd" are the same).
CodeLoop
CodeLoop(model: ChatModel, sandbox: Sandbox, *, system: str, first: str, emit: Callable[[dict[str, Any]], None], status: Callable[[], Mapping[str, Any] | None], reply_tokens: int | None = None, max_history_turns: int = 24, max_feedback: int = 8000, max_context_chars: int | None = 80000)
Messages, the sandbox, pending images and the usage totals of one model working on one problem.
vision
instance-attribute
vision = bool(getattr(model, 'vision', False))
max_context_chars
instance-attribute
max_context_chars = max_context_chars
messages
instance-attribute
messages: list[dict[str, Any]] = [{'role': 'system', 'content': system}, {'role': 'user', 'content': first}]
images
instance-attribute
images: list[dict[str, Any]] = []
model_errors
instance-attribute
last_code
instance-attribute
last_code: str | None = None
show
show(image: Any, note: str = '') -> None
exceeded
exceeded(budget: Budget, steps: int = 0) -> str | None
step
step(remaining: Mapping[str, Any], extra: Mapping[str, Any] | None = None) -> dict[str, Any]
One turn: ask the model, run its block, append the feedback. Returns the turn record (also emitted).
prompt_digest
prompt_digest(messages: list[Mapping[str, Any]]) -> str