Agent loops
Choose the logic that calls the model, uses tools and manages the conversation.
An agent loop decides what the model sees, when to call tools, and when the agent has finished answering. Brain runs that loop and saves its progress. You can use a ready-made loop or write your own.
Use a ready-made loop
The quickstart uses Pi:
import { pi } from "@aexhq/agentloop-pi";
const loop = pi();Pi uses brainEnv({ name: "brain" }) by default. Pass options such as
pi({ contextWindow: 64_000 }), or supply { env } to choose another Environment.
Pass loop as agentloop when creating a session. Pi handles model calls, parallel tool calls,
and summarizing older conversation when the context fills up. The
Codex loop is another option.
These are loop implementations for Brain, not the Pi or Codex applications. Their package guides describe supported behavior and options.
Customize behavior
Start with a session's system prompt and the loop's options. Write a custom loop when you need
different context selection, tool sequencing or stopping rules. A loop can also respond to results
from background tools without a new user message.
The authoring guide includes JavaScript, Rust and Python examples and build instructions. The loop API describes saved state and model/tool calls.