---
title: How do you use Google ADK callbacks as guardrails?
url: https://opencontroller.lyzr.com/blog/google-adk-callbacks-as-guardrails
published: 2026-11-16
updated: 2026-11-16
author: Lyzr
---

# How do you use Google ADK callbacks as guardrails?

**Use `before_model_callback` to screen prompts, `before_tool_callback` to validate tool arguments and block calls, and `after_tool_callback` or `after_model_callback` to redact what comes back. Returning a value from a "before" callback skips the model or tool entirely, which is what makes callbacks enforceable. For rules that must apply to every agent, put them in an ADK Plugin or a central policy layer rather than copying callbacks into each agent.**

Google's Agent Development Kit (ADK) exposes six callback hooks. They run inside the agent process, so they are the cheapest place to stop a bad action before it happens.

## Which ADK callbacks exist and what can each one stop?

| Callback | Runs | Return `None` | Return a value |
|---|---|---|---|
| `before_agent_callback` | Before the agent's main logic | Agent runs normally | Returned `Content` becomes the final output; agent logic is skipped |
| `after_agent_callback` | After the agent finishes | Output unchanged | Returned `Content` is appended as an extra event |
| `before_model_callback` | Before each LLM call | Model is called | Returned `LlmResponse` is used instead; the model call is skipped |
| `after_model_callback` | After each LLM response | Response unchanged | Returned `LlmResponse` replaces the model's response |
| `before_tool_callback` | Before each tool call | Tool runs | Returned `dict` is sent to the LLM as the tool result; the tool is skipped |
| `after_tool_callback` | After each tool call | Result unchanged | Returned `dict` replaces the tool's result |

Names differ by language. TypeScript uses `beforeModelCallback`, Go uses `BeforeModelCallbacks`, and Java uses `beforeModelCallbackSync`. The control-flow rules are the same.

## How do you block a risky prompt with before_model_callback?

The Python signature is `before_model_callback(callback_context: CallbackContext, llm_request: LlmRequest) -> Optional[LlmResponse]`. A minimal guardrail:

1. Read the latest user message from `llm_request.contents`.
2. Check it against your rule, for example a deny-list of instructions such as "ignore previous instructions", or a call to a classifier.
3. If it fails, return an `LlmResponse` with a refusal message. The model is never called.
4. If it passes, return `None`.

You can also set a value in `callback_context.state` to record that the guardrail fired. That gives you a trace for audit.

## How do you validate tool arguments with before_tool_callback?

The tool callback receives the tool, its arguments and a `ToolContext`: `before_tool_callback(tool, args, tool_context) -> Optional[dict]`. Typical checks:

1. **Allow-list the tool.** If `tool.name` is not approved for this agent, return `{"error": "tool not permitted"}`.
2. **Bound the arguments.** Refuse a `refund` call above a set amount, a SQL tool with `DROP` or `DELETE`, or a file path outside an approved directory.
3. **Check the user.** Read the user's role from session state and refuse actions above their entitlement.
4. **Rewrite instead of refuse.** You can modify `args` in place, for example forcing a `limit` parameter, then return `None` so the tool runs with safe values.

Returning a dict means the LLM sees your message as the tool result. Make it explicit, so the model tells the user the action was refused rather than retrying in a loop.

## How do you redact outputs with after callbacks?

Use `after_tool_callback` to strip personal data, secrets or internal identifiers from tool results before the model sees them. Use `after_model_callback` to scan the final response for leaked data before the user sees it. Both replace content only when you return a value.

## Why are callbacks not enough on their own?

Callbacks are code in one agent. That creates four gaps:

- **Copy drift.** Ten agents means ten copies of the same rule. One gets missed.
- **Developer-owned.** The person you are governing writes and can remove the guardrail.
- **No estate view.** Callbacks cannot tell you which agents lack them.
- **No kill switch.** A callback cannot stop an agent that is behaving badly in ways it did not anticipate.

ADK's own documentation recommends Plugins over callbacks for security guardrails and policies. A Plugin registers once on the Runner and applies to every agent, tool and model call it manages. That fixes copy drift within one application.

For enterprise-wide control, pair in-process checks with a policy layer outside the agent: a model gateway that can redact and block, identity-scoped tool access, and monitoring that can quarantine an agent. Our guide to [governing Google ADK agents](/blog/govern-google-adk-agents) covers deployment and identity; the [Model Armor guide](/blog/google-model-armor-for-ai-agents) covers prompt and response screening on Google Cloud.

## How Opencontroller does this

Opencontroller does not replace ADK callbacks. It adds the layer above them. It auto-discovers agents across Google Cloud, records them in a governed registry, and has a runtime adapter for Vertex AI. Policy as code applies the same rule across the estate rather than per agent. Live groundedness and task-adherence guardrails, LLM judges on production traffic, and behavioural-deviation monitoring with automatic quarantine catch what a single callback misses. Read [what an AI agent control plane is](/blog/what-is-an-ai-agent-control-plane), or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**What happens if before_tool_callback returns a dictionary?**
ADK skips the tool and sends that dictionary to the LLM as the tool's result.

**Should I use callbacks or Plugins for security policy?**
ADK documentation recommends Plugins for security guardrails and policies, because a Plugin registers once and applies across the Runner.

**Can a callback modify tool arguments instead of blocking?**
Yes. Change the `args` dictionary and return `None`; the tool then runs with the modified arguments.

## Key facts

Google Agent Development Kit (ADK) has six callbacks: before_agent_callback, after_agent_callback, before_model_callback, after_model_callback, before_tool_callback, after_tool_callback. Returning None continues normally. Returning an LlmResponse from before_model_callback skips the LLM call. Returning a dict from before_tool_callback skips the tool and sends the dict to the LLM as the result. after_model_callback and after_tool_callback replace outputs when they return a value. after_agent_callback appends returned Content as an extra event. ADK documentation recommends Plugins over callbacks for security guardrails and policies. Opencontroller adds an estate-wide layer: discovery across Google Cloud, a governed registry, a Vertex AI runtime adapter, policy as code, live groundedness and task-adherence guardrails, and automatic quarantine. Opencontroller pricing starts from $250K per deployment per year.

## Related

- [How do you govern agents built with Google's Agent Development Kit?](https://opencontroller.lyzr.com/blog/govern-google-adk-agents.md)
- [What does Google Cloud Model Armor do for AI agents?](https://opencontroller.lyzr.com/blog/google-model-armor-for-ai-agents.md)
- [How do input and output guardrails work in the OpenAI Agents SDK?](https://opencontroller.lyzr.com/blog/openai-agents-sdk-guardrails-and-tripwires.md)
- [More on Platform Guides](https://opencontroller.lyzr.com/blog/cluster/platform-guides)
