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.
Tools¶
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
¶
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
¶
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.
Budget¶
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).
Harness¶
harness ¶
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.
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.
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.
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.