ADK Callbacks: Intercepting Agent, Model, and Tool Execution

GCP Study Hub
November 15, 2026

In ADK, callbacks let you run your own code at fixed points in an agent's loop. Because callbacks are code rather than model instructions, they are enforced no matter what the model decides. On the Professional Agentic Architect exam, this topic centers on two distinctions: choosing the right callback to enforce a rule without modifying a tool function, and deciding when a policy belongs inside agent code versus outside the agent in Agent Gateway with Model Armor.

The six callback points in the ADK loop

ADK provides six callbacks arranged in pairs around the three stages of execution:

  • before_agent_callback and after_agent_callback run before and after the agent runs.
  • before_model_callback and after_model_callback run around each model call.
  • before_tool_callback and after_tool_callback run around each tool call.

This code-based approach differs from writing rules into an agent's instructions. An instruction depends on the model following it, so it is not enforced in code. When a rule must be enforced regardless of model behavior, placing the check in a callback ensures it runs as part of the loop.

How before_tool_callback and after_tool_callback differ

The tool callbacks are the most direct way to work with individual tool calls. Before a tool runs, ADK calls before_tool_callback and passes it the tool, its arguments, and the tool context, so the callback can inspect exactly what the model asked for.

The return value of before_tool_callback determines what ADK does next:

  • If the callback returns None, the tool runs normally.
  • If the callback returns a dict, ADK skips the tool and uses that dict as the tool's result.

Because ADK hands that dict to the model as the tool result, whatever the callback puts in it becomes information the model can use in its reply. The callback is also separate from the tool function, so adding a check this way leaves the tool's own code as it is.

The two tool callbacks differ in timing. before_tool_callback runs before the tool, which makes it the point where a call can still be skipped. after_tool_callback runs only after the tool has finished, which means any action the tool performs has already taken place by the time the callback executes.

When policy belongs in Agent Gateway and Model Armor

Callbacks are effective for in-code checks, but they place policy inside the agent's codebase. When a security team wants a centrally managed policy with no changes to agent code, such as prompt screening, a callback is not the right boundary. That kind of policy lives outside the agent, in an Agent Gateway that uses a Model Armor template, where the security team can manage it centrally.

A gateway's mode determines which traffic it sees. A Client-to-Agent gateway sits in front of an agent, such as one served on Agent Runtime, and handles traffic coming in from users. An Agent-to-Anywhere gateway governs the agent's outbound traffic to tools and other agents.

The Model Armor template attached to a gateway defines the filters it applies, such as prompt injection and jailbreak detection. Agent Gateway works with Model Armor templates rather than floor settings, and a template has to be in the same region as the gateway that uses it, because a template in one region cannot serve a gateway in a different region.

Calling Model Armor's sanitize API from an ADK callback is still possible, but it keeps the policy inside the agent's code rather than under central management. A direct call to the sanitize API also makes Model Armor a detector only, so the callback code has to decide what to block.

Choosing between callbacks and a gateway

The deciding question is who owns the policy. A callback fits a check that belongs to one agent's logic and must run at a specific stage of its loop, such as inspecting tool arguments in before_tool_callback before a call runs. A gateway with a Model Armor template fits a policy that a security team manages centrally, outside any agent's code, and it applies to whichever traffic the gateway's mode puts in its path.

Our Professional Agentic Architect practice exams test ADK callbacks alongside Agent Gateway and Model Armor policies, with detailed explanations that drill these distinctions.

Get tips and updates from GCP Study Hub

arrow