Skip to content

Building blocks

The ready building blocks from modular_agents.composites, which implement the interfaces.

Agent

composed_agent

ComposedAgent: a model plus a harness, owning one context that every run continues.

blueprint

Blueprint: everything an agent is made of, so the same agent can run on its own or serve as another agent's tool.

Blueprint dataclass

agent
agent(session_budget: Budget | None = None) -> Agent

A standalone agent, owning one context across runs.

as_tool
as_tool(name: str | None = None) -> AgentTool

The same agent as a tool: every call is a fresh context, charged to the caller's budget.

tree
tree() -> str

The composition as a tree: harness parts, plain tools, and agent tools expanded recursively.

AgentTool

Bases: SpawnAgentTool

Any blueprint as a named tool. From the caller's side it is indistinguishable from a device button.

Loop

loop

StandardLoop: advance a RunState one model call at a time until a stop condition.

StandardLoop

Bases: Loop

step async
step(state: RunState, ctx: RunContext) -> RunOutcome | None

One model call, plus the tool calls it asks for. Returns an outcome when the run is over.

Budget

tree_budget

TreeBudget: a budget whose spend is also charged to its ancestors.

Model

anthropic_model

AnthropicModel: an EndpointModel that builds its own Anthropic endpoint (adapter + HTTP transport).

endpoint_model

EndpointModel: one model name on one endpoint. Waits for capacity, calls the endpoint, fills in elapsed time and cost.

adapter_endpoint

AdapterEndpoint: an endpoint made of a protocol adapter and a transport.

anthropic_adapter

AnthropicAdapter: common representation <-> Anthropic Messages API.

http_transport

HttpTransport: POSTs JSON to one URL. Streams are read as server-sent events.

scheduler

SemaphoreScheduler: caps concurrent requests per endpoint and retries transient errors centrally.

playbook_model

PlaybookModel: a fake model that follows a written playbook instead of thinking. No API, fully deterministic.

PlaybookModel

Bases: Model

Picks the playbook whose keyword is in the task, then plays one turn per model call.

Stateless: the position in the playbook is read from the messages, so one instance serves any number of contexts.

Tools

static_toolset

StaticToolSet: a fixed list of tools.

combined_toolset

CombinedToolSet: several tool sets and single tools, seen by the model as one.

spawn_agent

spawn_agent: a sub-agent is just a tool that runs a fresh context through a harness and returns its final answer.

tool_helpers

Small helpers shared by tools.

schema

schema(**props: str) -> dict

Object schema where every property is a required string.

signal

signal(ctx: ToolContext | None) -> AbortSignal

The run's signal, or one that is never set when the tool is called on its own.

Events

transcript_bus

TranscriptBus: prints tool calls and results as a tree, one line per event.

TranscriptBus

Bases: EventBus

Prints tool calls and results, indented by how deep the agent is nested and labelled with its path in the tree.

guard_bus

GuardBus: wraps any bus and adds safety rules on tool calls.

GuardBus

Bases: EventBus

Wraps another bus: events pass through unchanged, tool calls must get past every rule first.

clamp

clamp(
    tool: str,
    field: str,
    lo: float = -math.inf,
    hi: float = math.inf,
) -> Rule

Keeps one numeric input of a tool within bounds, by changing the call instead of denying it.

Context policy

live_status_policy

LiveStatusPolicy: shows the model the devices' current state on every call, without storing it in the context.

Inbox

list_inbox

ListInbox: an in-memory inbox for one agent.

Devices

device

Simulated devices: plain Python objects whose @action methods become tools.

Device

A toy piece of hardware. Its state lives here, outside any agent's context, so every agent sees the same world.

status
status() -> str

Report the current state.

ActionTool

Bases: Tool

One device method as a tool, named _.

DeviceToolSet

Bases: StaticToolSet

The actions of some devices. readonly=True gives a look-but-don't-touch view of the same devices.

action

action(fn=None, *, readonly: bool = False)

Marks a device method as a tool. Its docstring is the description, its parameters the input schema.

Helpers

helpers

Small helpers shared by implementations.

abortable async

abortable(aw: Awaitable[T], signal: AbortSignal) -> T

Await something, but give up as soon as the signal is set.

append_user

append_user(
    messages: list[Message], content: list[ContentBlock]
) -> None

Add user content, joining the last message if it is a user one, since roles must alternate.