Skip to content

Interceptors

Callbacks watch a run and can never change it. Interceptors decide: they see every model call and every tool call of an agent and may change it or stop it. Use them to mask secrets, forbid tools, cap arguments, or repair a malformed tool call from a small model.

from maki import Agent, Block, DenyTools, Interceptor, MaxArgChars, Redact

class NoShell(Interceptor):
    def before_tool(self, agent, call):
        if call.name.startswith("shell__"):
            return Block("shell access is off in this deployment")

agent = Agent("helper", backend,
              middleware=[Redact(), DenyTools(deny=["file_writer__*"]), MaxArgChars(4000), NoShell()])

The three hooks

All are optional. Return None to leave things as they are.

Hook Sees May return
before_model(agent, messages) the messages about to be sent, the system prompt first as Message("system", ...) a new list of Message, or Block(reason)
after_model(agent, response) the model's LLMResponse a changed LLMResponse (text, or tool_calls), or Block(reason)
before_tool(agent, call) one ToolCall (plugin__method and its arguments) a changed ToolCall, or Block(reason)
  • before_model changes what the model sees for that call only. The conversation memory keeps the original, and the hook runs again on every round.
  • Callbacks and the model spans of a run trace see what was actually sent (after before_model) and the response after after_model. Interceptors guard the model and the tools, not storage: the task text in on_task_start, the stored conversation memory and task history, and checkpoints keep the original text (a trace and a checkpoint apply their own masking; use Agent(trace=TraceCallback(path, include_content=False)) to keep content out of traces).
  • Hooks are plain synchronous methods; an async def hook returns a coroutine, which counts as an unusable return and stops the call.
  • Order: before_* hooks run in list order, after_model in reverse (like layers of an onion).
  • Interceptors can be called from several threads at once (parallel tool calls): keep them thread-safe.

They fail closed

An interceptor that raises, or returns something unusable, stops the call it was looking at. A rule that quietly stops applying is worse than a task that fails. Only a deliberate Block is not an error.

  • Blocked tool call: the task goes on. The model receives Error: tool call blocked by policy: <reason>. Do not repeat this call; use a different approach or answer without it. as the tool's result, so a small model is told why and not to retry the same call. The call is not run, and callbacks see on_tool_start then on_tool_error.
  • Failing interceptor in before_tool: the call is blocked the same way, with a generic reason (interceptor 'X' failed (ValueError); the exception text goes to the log, not to the model).
  • Blocked or failing model hook: the task raises InterceptorBlocked (a block) or InterceptorError (a failure), and nothing is sent.

Interceptors run before the agent's own tool checks (allowed methods, DANGEROUS_METHODS, argument types), so an interceptor can narrow what a tool may do and never widen it: rewriting a call into a dangerous method still needs allow_dangerous_tools=True.

Built-ins

Interceptor Does
Redact(patterns=(), replacement="***") masks credential-looking query parameters (?api_key=...) and your regular expressions in every message, the system prompt, tool-call arguments and the answer
DenyTools(deny=(), allow=None) blocks tools by plugin__method name (* and ? match); allow blocks everything not listed; deny wins
MaxArgChars(limit) blocks a call with a text argument longer than limit (it blocks instead of cutting: a cut argument changes what the tool does)

On a maki serve server

The same built-ins can be chosen by name in the server YAML. Nothing is imported from the config, so a config file cannot run code.

middleware:
  - redact
  - deny_tools: {deny: ["file_writer__*"]}
  - max_arg_chars: 4000

Tool calls made on the server therefore pass the interceptors, also when the client is an AgentProxy on another machine. A bad middleware: section fails at startup, before the backend is built.

Over HTTP a blocked model call answers 403 with the reason the interceptor gave, and a failing interceptor 500 (details stay in the server log). /stream answers 409 when an interceptor checks responses (after_model, which includes redact): use /execute there.

Limits

  • Model hooks need typed messages. A backend that does not declare supports_typed_messages (all built-in backends do) cannot run before_model/after_model; the agent refuses to run its tool loop unguarded and raises InterceptorError. Tool hooks work everywhere.
  • Streaming: stream_task runs before_model but refuses any after_model interceptor (including Redact, which also masks answers), because the text has left before it can be checked. Use execute_task.
  • AgentManager's own synthesis calls (final_synthesis) are not agent calls and are not intercepted; the agents it runs are.
  • AgentProxy has no client-side interceptors yet; configure the server's middleware: instead.
  • Interceptors are not saved in a checkpoint: pass them again when you create the agent that resumes.