Skip to content

Interfaces

The interfaces and the data types between them, from modular_agents.interfaces. One module per concept, with no implementations.

Messages

messages

Messages: the conversation format shared by the context, the loop, tools and the model layer.

Every block may carry ext: provider data keyed by protocol name, e.g. {"anthropic": {"cache_control": ...}}. An adapter sends its own key's data and ignores the others, so a context can move to another provider without leaking wire formats.

OpaqueBlock

Bases: TypedDict

Provider-only content with no common form, e.g. a server-side web search and its results. Kept for the protocol that produced it, dropped by the others.

Model

model

Model access layer, public side: what the rest of the system sees.

Prompt dataclass

What one model call is asked. Built by the loop from the state and the tools, then adjusted by the context policy.

Model

Bases: Resource, ABC

One model, ready to be requested: one model name on one endpoint, with its params.

Tools

tools

Tools interface: a definition the model sees, plus execute().

Tool

Bases: Resource, ABC

execute abstractmethod async
execute(
    input: dict, ctx: ToolContext | None = None
) -> ToolOutput

ctx is None when the tool is called on its own, outside a loop.

ToolSet

Bases: Resource, ABC

definitions abstractmethod
definitions() -> list[ToolDefinition]

Asked on every step, so the set may change during a run (e.g. an MCP server's tool list).

State

state

Run state: the context of one run, as plain serializable data.

InitialState dataclass

What a new context starts with. Turned into a RunState once, when the agent is created.

Run store

store

Run store: persists run states, so runs can be inspected or resumed.

Context policy

policy

Context policy: decides what the model sees of the run state, and when the state itself is compacted.

ContextPolicy

Bases: ABC

prepare abstractmethod async
prepare(
    state: RunState, prompt: Prompt, model: Model
) -> Prompt

What the model sees on this call: truncation, cache breakpoints, extra system prompt, tool filtering. Gets the prompt built from the state and returns a changed copy; never changes the state.

compact async
compact(
    state: RunState,
    model: Model,
    budget: Budget,
    signal: AbortSignal,
) -> None

Rewrite state.messages for good, e.g. replace old history with a summary made by the model. Model calls made here are charged to the budget.

Loop

loop

Loop interface: advances a state until a stop condition, and what it gets and returns.

RunContext dataclass

Everything one run needs besides its state. Built for each run, so the loop never depends on the harness.

Loop

Bases: ABC

run abstractmethod async
run(state: RunState, ctx: RunContext) -> RunOutcome

Advance the state until a stop condition.

Budget

budget

Budget interface and the budget amounts it works with.

Budget

Bases: ABC

charge abstractmethod
charge(usage: Usage) -> None

Also charged to all ancestors.

child abstractmethod
child(limit: BudgetLimit) -> Budget

Capped by the remaining parent budget.

Events

events

Events interface: observation (emit) and interception are separate.

Interception points and what a decision means at each: - prompt: a task is about to be added to the context. Deny blocks the run, Modify replaces the content. - tool_call: before a tool runs. Deny answers the call with an error, Modify replaces the input. - tool_result: after a tool ran. Deny replaces the result with an error, Modify replaces the content. - stop: the model ended its turn. Deny keeps the run going, the reason is given to the model as the next message. - compact: the policy is about to compact the context. Deny skips it. A deny's reason is shown to the model (or to the user, for prompt).

EventBus

Bases: ABC

emit abstractmethod
emit(e: LoopEvent) -> None

Observation: logging, tracing, UI.

intercept abstractmethod async
intercept(e: InterceptableEvent) -> Decision

Permissions, approval, hooks.

Harness

harness

Harness: everything around the model. The loop, the budget and the optional parts.

Harness dataclass

run async
run(
    state: RunState,
    model: Model,
    signal: AbortSignal,
    inbox: Inbox | None = None,
) -> RunOutcome

Hand the harness parts to the loop as one RunContext.

Agent

agent

Agent interface: an agent owns one context and advances it.

Agent

Bases: Resource, ABC

Use as async with agent: so its tools and model are started and closed.

state abstractmethod property
state: RunState

The agent's context. Read-only by convention.

inbox abstractmethod property
inbox: Inbox

Content put here while a run is going is added to the context before the next model call.

run abstractmethod async
run(
    task: str | list[ContentBlock], signal: AbortSignal
) -> RunOutcome

Append task to the context, then run the loop until a stop condition.

Inbox

inbox

Inbox: content that arrives while a run is going, e.g. a message typed by the user or a background task's result.

Inbox

Bases: ABC

put abstractmethod
put(content: list[ContentBlock]) -> None

Called from outside the loop: the UI, a background task.

take abstractmethod
take() -> list[ContentBlock]

Everything waiting, in order; the inbox is empty afterwards. The loop calls it between steps.

Cancellation

cancellation

Cancellation: one signal crosses every async boundary of a run.

Cancelled

Bases: Exception

Raised when an AbortSignal is set.

Lifecycle

lifecycle

Lifecycle: parts that hold connections or processes (an MCP server, an HTTP client, a container) start and close.

Resource

start() before the first run, aclose() after the last. Parts are shared between agents, so both may be called more than once and must be idempotent. The defaults do nothing, so stateless parts need not care.

Endpoint

endpoint

Model access layer internals: one reachable model service, made of a protocol adapter and a transport.

RetryableError

Bases: Exception

A transient infrastructure failure (rate limit, overload, network) that may be retried.

Endpoint

Bases: Resource, ABC

A reachable model service: one protocol, one transport, its own cost model.

ProtocolAdapter

Bases: ABC

Pure data to data: the common representation <-> one protocol's wire format.

Scheduler

routing

Model access layer internals: how requests to an endpoint are paced.

Environment and command runner

env

Environment: where environment-bound tools run their commands. Plain data.

runner

Command runner: runs a shell command in an environment.