Tool Guardrails
Tool guardrails are an experimental feature. Their API and behavior might change in future versions.
In a Quarkus application, the builder methods and annotations described on this page are not applied yet: the tools
run without these guardrails, and no error tells you so. This also applies to AI Services that you build with
AiServices.builder(..) and to agents that you build with AgenticServices.agentBuilder(..): when the Quarkus
LangChain4j extension is present, it builds them, with its own tool loop. The
Quarkus LangChain4j extension has its own
tool guardrails. Support for the tool guardrails of LangChain4j is tracked in
quarkiverse/quarkus-langchain4j#3162. See also
Differences from the Quarkus tool guardrails.
Tool guardrails validate or rewrite what goes into a tool and what comes out of it. They are a sibling of the guardrails that validate the user message and the LLM response of an AI Service, and they work the same way: a guardrail returns a success, a rewrite or a failure, and fires an event that you can listen to.
- A tool input guardrail runs before a tool is executed. It sees the arguments the LLM chose and can accept them, rewrite them or reject the call.
- A tool output guardrail runs after a tool was executed. It sees the result of the tool, including an error result, and can accept it, rewrite it or reject it, before the LLM sees it.
Why tool guardrails
The main motivation is indirect prompt injection. The text that a tool returns (a fetched web page, a search snippet, the answer of an MCP server, even an exception message that echoes remote content) goes straight to the LLM, and the LLM may treat instructions hidden in it as if they came from the user. A tool output guardrail is the place to detect, block or redact such content.
Tool input guardrails are the other half: the LLM chooses the arguments of a tool call, so they are untrusted too. An input guardrail can enforce limits (an allowed host, a maximum amount), normalize values, or block a call altogether.
Tool guardrails work the same way for every kind of tool:
@Toolmethods- tools specified programmatically (
tools(Map),tools(List<AiServiceTool>)) - tools returned by a
ToolProvider - MCP tools
and in every mode of an AI Service: blocking, CompletableFuture, TokenStream and reactive
(see Non-blocking and Reactive).
See Which tools are guarded for the built-in tools, and for the tools that tool guardrails do not see.
Getting started
This complete program gives an AI Service a tool that fetches web pages, and an input guardrail that lets the LLM fetch
only the pages of one site. It needs the langchain4j and langchain4j-open-ai modules (see
Get Started):
import static dev.langchain4j.model.openai.OpenAiChatModelName.GPT_4_O_MINI;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.guardrail.ToolInputGuardrail;
import dev.langchain4j.guardrail.ToolInputGuardrailRequest;
import dev.langchain4j.guardrail.ToolInputGuardrailResult;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
public class ToolGuardrailsExample {
static class WebTools {
@Tool(name = "fetch_page", value = "Fetches a web page")
String fetchPage(@P(name = "url", value = "The URL of the page") String url) {
return "<html>...</html>"; // fetch the page here
}
}
static class AllowedHostsInputGuardrail implements ToolInputGuardrail {
@Override
public ToolInputGuardrailResult validate(ToolInputGuardrailRequest request) {
if (!request.toolName().equals("fetch_page")) {
return success(); // this guardrail only guards one tool
}
String url = String.valueOf(request.argumentsAsMap().get("url"));
return url.startsWith("https://docs.example.com/")
? success()
: failure("Only pages of https://docs.example.com can be fetched. Do not retry with another host.");
}
}
interface Assistant {
String chat(String userMessage);
}
public static void main(String[] args) {
ChatModel chatModel = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName(GPT_4_O_MINI)
.build();
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.tools(new WebTools())
.toolInputGuardrails(new AllowedHostsInputGuardrail())
.build();
System.out.println(assistant.chat("Summarize the page https://example.com/news"));
}
}
When the LLM calls fetch_page with https://example.com/news, the guardrail rejects the call: the tool does not
run, and the LLM gets the message of failure(..) as the result of the tool. The LLM then usually tells the user that
it cannot fetch that page. Ask for a page of https://docs.example.com/ instead, and the tool runs.
The rest of this page explains what a guardrail can do with the arguments of a call (Tool Input Guardrails) and with the result of a tool (Tool Output Guardrails), and the other ways to declare guardrails (Declaring tool guardrails).
The examples on this page implement validate, which is all that a blocking AI Service, or one that returns a
TokenStream, needs. An AI Service that returns a CompletableFuture or a Flow.Publisher calls validateAsync
instead, and a guardrail that does not implement it fails the invocation the first time it runs. See
Non-blocking and reactive modes.
Tool Input Guardrails
A tool input guardrail implements
ToolInputGuardrail
and its validate method, which receives a
ToolInputGuardrailRequest,
like the guardrail of Getting started:
public class AllowedHostsInputGuardrail implements ToolInputGuardrail {
@Override
public ToolInputGuardrailResult validate(ToolInputGuardrailRequest request) {
if (!request.toolName().equals("fetch_page")) {
return success(); // this guardrail only guards one tool
}
String url = String.valueOf(request.argumentsAsMap().get("url"));
return url.startsWith("https://docs.example.com/")
? success()
: failure("Only pages of https://docs.example.com can be fetched. Do not retry with another host.");
}
}
fetch_page is the name of the tool, and url the name of its parameter, as declared in the WebTools class of
Getting started. The name of a @Tool method is the method name, unless
@Tool(name = ...) sets another one.
The request gives access to:
| Accessor | Description |
|---|---|
toolName() | The name of the tool, as the LLM sees it. |
arguments() / argumentsAsMap() | The arguments the LLM chose, as the raw JSON string or as a read-only map. They are untrusted. For a call without arguments, arguments() is null and argumentsAsMap() an empty map. |
toolExecutionRequest() | The whole tool execution request. |
toolSpecification() | The specification of the tool, as the LLM sees it. Never null: for a tool that LangChain4j knows only by its name (a tool that a framework runs without its specification), it holds only the name. |
toolSource() | Where the tool comes from. See Telling the tools apart. |
invocationContext() / memoryId() | The context of the AI Service invocation (invocation id, chat memory id, method arguments, ...). |
requestParams() | The GuardrailRequestParams of the AI Service invocation. |
Tool Input Guardrail Outcomes
There are helper methods on the ToolInputGuardrail interface for the outcomes:
| Outcome | Helper method | Description |
|---|---|---|
| success | success() | - The arguments are valid. - The next guardrail in the chain is executed, then the tool. |
| success with rewrite | successWith(String) | - The argument is a JSON object, the new arguments of the tool call. null means no rewrite (a plain success). Anything else (malformed JSON, an array, a number) is treated as a fatal failure of the guardrail: the invocation is aborted with a ToolInputGuardrailException before the tool runs.- The following guardrails and the tool receive the rewritten arguments, and so do the output guardrails, the afterToolExecution callback and the events. beforeToolExecution runs before the guardrails and sees the original arguments.- The chat memory keeps the tool call as the LLM made it: the memory is the conversation as the LLM said it, and the events are what was executed. The tool result the LLM gets starts with a note that the arguments were changed (see What the LLM is told about a rewrite). The tool call is stored before the tool runs, so an AI Service output guardrail, which checks the final answer, cannot keep it out of the stored history: to filter what is stored, use a custom ChatMemory / ChatMemoryStore. Put values that only the tool needs in InvocationParameters, not in the arguments. |
| success with rewritten arguments | successWithArguments(Map<String, ?>) | - Like success with rewrite, with the new arguments given as a map of argument names to values. The map is serialized to a JSON object with the same JSON codec argumentsAsMap() parses with, so quotes and other special characters need no escaping (see Rewriting the arguments).- Unlike successWith(String), null is not a plain success: a null map, or a value that has no JSON representation, is a fatal failure of the guardrail, so a redacting guardrail that returns null by mistake never lets the original arguments through. |
| failure | failure(String) or failure(String, Throwable) | - The call is rejected and the tool is not executed. - The remaining guardrails of the chain still run, to accumulate all the failures. - The message is sent to the LLM as an error tool result. See Failures. |
| fatal | fatal(String) or fatal(String, Throwable) | - The call is rejected, the tool is not executed, the remaining guardrails do not run, and the AI Service invocation is aborted with a ToolInputGuardrailException. See Fatal failures. |
Rewriting the arguments
Read the arguments as a map, change them and return the map with successWithArguments(..). Building the JSON by
hand, for example with String.format, breaks as soon as a value contains a quote:
public class EmailMaskingInputGuardrail implements ToolInputGuardrail {
private static final Pattern EMAIL = Pattern.compile("[\\w.+-]+@[\\w-]+\\.[\\w.]+");
@Override
public ToolInputGuardrailResult validate(ToolInputGuardrailRequest request) {
var arguments = new HashMap<>(request.argumentsAsMap());
arguments.computeIfPresent("query", (name, query) -> EMAIL.matcher(query.toString()).replaceAll("<email>"));
return successWithArguments(arguments);
}
}
The tool, the following guardrails, the output guardrails, afterToolExecution and the events see
the rewritten arguments. The chat memory keeps the tool call as the LLM made it.
What the LLM is told about a rewrite
Because the chat memory keeps the call as the LLM made it, the LLM would otherwise read, on every later turn, that the
call it made is the one that ran: if a guardrail caps transferMoney(amount = 10000) to amount = 1000, the LLM would
believe that 10000 were transferred, and may tell the user so. So when an input guardrail changed the value of an
argument the LLM sent and the tool ran, the tool result the LLM gets starts with a note, followed by the result of the
tool as it is:
<tool_arguments_changed>The arguments of this call were changed before it ran: {"amount":1000}</tool_arguments_changed>
Transfer done
- The note shows only the values the LLM sent that changed, with their new values (a removed one as
null). An object argument is compared field by field, at any depth, and the note shows only its changed fields, nested where they are nested: fortransfer(TransferRequest request),{"request":{"amount":1000}}. A list is compared, and shown, as a whole. Anullor a plain value of the LLM that a guardrail replaced with an object is shown with that whole object. A key that the LLM did not send, and that a guardrail added, is not shown, except inside a list or an object that is shown whole. The LLM decides which keys it sends, though: see the warning below. The values are compared parsed, so a rewrite that only changes the spacing or the order of the keys (as re-serializing a map can) adds no note, and numbers are compared by their value (500and500.0are the same). Arguments of the LLM that cannot be parsed, and values a guardrail only added, give no note. - The note is in the text of the result (its first text content), not in a content of its own: some LangChain4j integrations (OpenAI Chat Completions, Mistral, Ollama) accept only a single text content in a tool result. A result without text (only an image, for example) gets the note as a text content of its own, in front of the other contents. Those integrations reject a result without text anyway, and the ones that accept it (Anthropic, Amazon Bedrock, Google AI Gemini, Google GenAI, OpenAI Responses) accept the extra text content too. The Google AI Gemini integration sends only the last text content of a tool result that has several, so with a tool that returns more than one text content, the note (like every text but the last) does not reach the LLM there.
- It is added after the output guardrails, so they check the result of the tool, not the note, and no guardrail sees
the note, not even the output guardrails of the
AiServicesbuilder, which run last. An error result of the tool gets it too, and so does the text that replaces the result when the call is rolled back. Thefailure(..)message of an output guardrail gets it in front of it as well, since the changed arguments may be why the result was rejected. Only a call that an input guardrail rejected gets the message of the guardrail without a note: nothing ran. - The events,
toolExecutions(),afterToolExecutionand the return value of an immediate tool get the result without the note.
The note is set by the ToolInputGuardrailsConfig of the AI Service. Change its template, a
PromptTemplate with the variables {{arguments}} (the changed arguments) and
{{result}} (the text of the result, which the template must contain), for example to put the note after the result,
or turn it off:
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.tools(new BankTools())
.toolInputGuardrails(new TransferLimitInputGuardrail())
.toolInputGuardrailsConfig(ToolInputGuardrailsConfig.builder()
.argumentsRewriteNoteTemplate(PromptTemplate.from("{{result}}\n(The transfer ran with {{arguments}}.)"))
// or .argumentsRewriteNoteEnabled(false)
.build())
.build();
The two variables are filled in one pass, so {{arguments}} or {{result}} inside the arguments or the result stays
as it is. A template that leaves out {{result}}, or uses a variable other than these two and the date and time
variables of PromptTemplate, is rejected by argumentsRewriteNoteTemplate(..). When the configuration comes from a
framework rather than from ToolInputGuardrailsConfig.builder(), such a template fails when the AI Service is built.
The note of a call is rendered once, so the date and time variables give the time the tool call ended, also in the
text that replaces the result when the call is rolled back.
The note is sent to the LLM and stored in the chat memory, and it shows the new value of every argument the LLM sent
that a guardrail changed. The LLM decides which keys it sends, and a prompt injection can make it send any key. When
the LLM sends a key that your guardrail sets (a token, for example), the value of the guardrail replaces a value of
the LLM, and the note shows it. A value that a guardrail puts inside an object that replaces a null or a plain value
of the LLM is shown too, with that object. So never put a value that the LLM must not see in the arguments: pass it in
InvocationParameters instead. If a guardrail must still put such a value in
the arguments, leave {{arguments}} out of the template, or turn the note off.
The capped transfer above shows what the note does, but a rewrite is not a good way to enforce a limit. The LLM is
told that 1000 were transferred, not why, and it may call the tool again for the rest of the amount. To enforce a
limit, reject the call with failure(..) and a message that gives the reason and the next step, for example
failure("Transfers are limited to 1000. Tell the user, and do not split the transfer."). Keep rewrites for
normalizing values and for redacting data.
Arguments that cannot be parsed
The LLM sometimes produces arguments that are not valid JSON. argumentsAsMap() then throws a
ToolArgumentsException. A guardrail does not need to catch it: the tool is not executed, and the call is handled
like any other arguments error, by the
ToolArgumentsErrorHandler of the AI Service. The handler decides
whether the invocation goes on. When you do not set one, it depends on the mode of the AI Service:
- blocking and
TokenStream: the default handler fails the invocation; CompletableFutureandFlow.Publisher: the error is sent to the LLM, which can correct the arguments.
To send the error to the LLM in every mode, set
.toolArgumentsErrorHandler(ToolArgumentsErrorHandler.sendExceptionMessageToLlm()) on the AI Service builder. The
handler, the chat memory and the
events see the tool call as the LLM sent it, even when an earlier guardrail rewrote its arguments.
Any other exception thrown by a guardrail aborts the invocation, like a fatal failure.
Tool Output Guardrails
A tool output guardrail implements
ToolOutputGuardrail,
which receives a
ToolOutputGuardrailRequest:
public class InjectionOutputGuardrail implements ToolOutputGuardrail {
private static final Pattern INJECTION = Pattern.compile(
"ignore (all )?(previous|prior) instructions", Pattern.CASE_INSENSITIVE);
@Override
public ToolOutputGuardrailResult validate(ToolOutputGuardrailRequest request) {
return INJECTION.matcher(request.text()).find()
// the message does not quote the content: that would pass the injection to the LLM anyway
? failure("The tool result contains instructions and was blocked. Do not retry.")
: success();
}
}
The output request has the same accessors as the input request (toolName(), arguments(), toolSource(), ...),
where arguments() are the arguments the tool was executed with, and adds:
| Accessor | Description |
|---|---|
resultContents() | All the contents of the tool result (text, images, ...), see Multi-content results. |
text() | The texts of all the text contents, joined with a newline. It never throws, unlike ToolExecutionResult.resultText(), and is an empty string if there is no text content. |
isError() | Whether the result is an error result: the tool threw, an MCP server answered with isError, or an earlier guardrail rewrote it into an error. See Error results. |
An output guardrail does not see the attributes() of the result (for an MCP tool, the entries of the _meta field
of the response of the server, which the McpToolProvider keeps only with returnToolResultAttributes(true)), and
cannot change them. The attributes are not sent to the LLM, but they are stored in the chat memory. A rewrite keeps
them, and a failure(..) drops them.
Tool Output Guardrail Outcomes
| Outcome | Helper method | Description |
|---|---|---|
| success | success() | - The result is valid. - The next guardrail in the chain is executed, then the LLM sees the result. |
| success with rewritten text | successWith(String) | - Replaces the text contents of the result with the given text. Non-text contents and the isError flag are kept.- null means no rewrite (a plain success). To remove the text, prefer a short placeholder (such as "The content was removed.") to an empty text, which some LLM providers reject.- Use it to redact or sanitize: a sanitized success stays a success, and a sanitized error result stays an error (see Compensation). |
| success with rewritten contents | successWith(List<Content>, boolean isError) | - Replaces all the contents of the result and its isError flag.- The only way to turn a result into an error result, or an error result into a success, without a failure. The list must not be empty. |
| failure | failure(String) or failure(String, Throwable) | - The result is replaced by an error result with the message. The LLM never sees the original result. - The remaining guardrails of the chain still run, to accumulate all the failures. - See Failures. |
| fatal | fatal(String) or fatal(String, Throwable) | - The remaining guardrails do not run, and the AI Service invocation is aborted with a ToolOutputGuardrailException.- See Fatal failures. |
Output guardrails also see error results
Output guardrails check what the tool produced, success or error, including the results of its own error handlers:
they run once the executor of the tool has started. A result made before the tool runs skips them: the message of a
non-fatal input failure, the text of the ToolArgumentsErrorHandler for arguments that an input guardrail could not
parse (see Arguments that cannot be parsed), and the result for a tool name that
the LLM made up. This is the same line as for the AI Service guardrails, whose output guardrails only see what the
LLM produced.
A tool output guardrail runs on every result of a tool that is sent to the LLM, including error results
(isError() == true). An exception of the tool becomes such an error result when the
ToolExecutionErrorHandler sends it to the LLM, which the default handler does in
the blocking and TokenStream modes. In the CompletableFuture and Flow.Publisher modes, the default handler fails
the invocation instead, so the output guardrails do not run for that exception, unless the exception implements
ToolErrorVisibleToLlm: it then becomes an error result
with the text of its messageForLlm(), and the output guardrails check it (see the
defaults of these modes). The text of an
error result is often attacker-controllable too: an MCP server can put remote content in the error it returns, and
the message of an exception can echo what the tool fetched. A guardrail that is not interested in errors opts out:
@Override
public ToolOutputGuardrailResult validate(ToolOutputGuardrailRequest request) {
if (request.isError()) {
return success(); // error results are not checked
}
// ... check request.text()
return success();
}
successWith(String) keeps the isError flag of the result it rewrites, so sanitizing an error result can never
turn it into a success. Only successWith(List<Content>, boolean) can change the flag.
A tool that returns immediately is
guarded too. A call that a guardrail rejects does not return immediately: the LLM gets the failure message and is
called again. (The @OutputGuardrails of the AI Service do not run on an immediate return; tool guardrails do.)
What the AI Service returns depends on its return type:
-
Result<T>: the guarded result is inResult#toolExecutions(), asToolExecution#resultContents().ToolExecution#result()gives its text only when the result is a single text content, and throws anIllegalStateExceptionotherwise (for example after a rewrite that kept an image next to the text). -
void/Void: nothing, as without guardrails. -
Any other type: the AI Service returns the Java object that the
@Toolmethod returned,ToolExecution#resultObject(). An output guardrail that rewrites the result drops that object, since it no longer matches what the LLM sees, and returning it would bypass the rewrite right where the value reaches your code. So after a rewrite:- a
Stringmethod returns the text of the rewritten result (the texts of its text contents, joined with a newline when there are several), and fails with anIllegalConfigurationExceptionwhen the rewrite left no text (only non-text contents); - any other return type fails with an
IllegalConfigurationException, since there is no object of the tool to return.
Use
Result<T>to get the guarded result of a tool whose output a guardrail can rewrite, whatever its type.ToolExecution#isResultReplaced()tells a dropped object from a tool that returnednull: it istruewhen an output guardrail replaced the result of a tool that ran (also an error result of the tool), with a rewrite or with the message of afailure(..). - a
With any return type, the afterToolExecution callback, TokenStream#onToolExecuted and the ToolExecutedEvent
also get the guarded result. The ToolExecution of afterToolExecution and onToolExecuted also tells, with
isResultReplaced(), whether it was replaced.
Multi-content results
A tool result can contain several contents, such as text and images (see
Multimodal Tool Results). A tool output guardrail sees all of them in
resultContents(), and the text() helper never throws for non-text contents. To rewrite them, either replace
only the text with successWith(String), or build the new list of contents:
@Override
public ToolOutputGuardrailResult validate(ToolOutputGuardrailRequest request) {
List<Content> textOnly = request.resultContents().stream()
.filter(TextContent.class::isInstance)
.toList();
if (textOnly.size() == request.resultContents().size()) {
return success(); // nothing to remove
}
// a tool result always has contents, so say what happened when nothing is left
List<Content> rewritten = textOnly.isEmpty()
? List.of(TextContent.from("The tool returned only images, which were removed."))
: textOnly;
return successWith(rewritten, request.isError()); // keep the isError flag unless you mean to change it
}
The list given to successWith(List<Content>, boolean) must not be empty: an empty list makes the guardrail throw,
which aborts the invocation like a fatal failure.
Failures are error results for the LLM
A non-fatal failure (failure(...)) does not abort anything. It becomes an error tool result
(isError == true) that is sent to the LLM like any other tool result, so the LLM can correct itself: pick other
arguments, choose another tool, or give up. Many LLM providers have no error flag for tool results, so the message
may be all that the LLM gets: write it so that it reads as a rejection and says what to do next.
- The text of the error result is the failure message of the guardrail, verbatim. LangChain4j adds no prefix and no wrapper text, so the message is entirely under your control. If several guardrails of the chain fail, their messages are joined with a newline, in the order of the chain. The one exception: when an input guardrail changed the arguments and the tool ran, the note that says so comes before the message of an output guardrail.
- A failing input guardrail means the tool is not executed.
- A failing output guardrail means the tool was executed, and its real result is replaced.
- Like AI service guardrails, a chain keeps running after a non-fatal failure, and failures are aggregated.
A guardrail that costs money (for instance one that calls an LLM) still runs after an earlier guardrail failed.
Within one level (the builder, a class annotation, a method annotation or an
AiServiceTool), put the cheap and most selective guardrails first. The input guardrails given to the builder always run first, and the output ones always run last (see Resolution order). - Since the chain keeps running, the order of the guardrails does not keep rejected content away from a later
guardrail: every guardrail of the chain gets it, whether it runs before or after the one that failed. Only
fatal(..)stops the chain, and only a rewrite (successWith(..), orsuccessWithArguments(..)for the input) changes what the later guardrails get. Keep this in mind for a guardrail that sends what it checks to an external service or to another LLM.
Fatal failures
A fatal failure (fatal(...)) stops the chain and aborts the AI Service invocation:
- A
ToolInputGuardrailExceptionor aToolOutputGuardrailException(both extendGuardrailException) is thrown to the caller. With aCompletableFutureor a reactive AI Service, the future fails or the publisher signalsonErrorwith it; with aTokenStream, it goes toonError. It is never thrown from a callback thread. - It bypasses the
ToolExecutionErrorHandler: the handler is for errors of the tool itself, and a guardrail rejection is a policy decision that the LLM must not be able to negotiate with. - The LLM does not get a chance to retry.
The message of the exception names the tool, followed by the message of the guardrail (or of the exception that the
guardrail threw). LangChain4j does not put the arguments in it, since they can hold user data, but a guardrail message
that quotes what the guardrail checked does: exceptions end up in logs, so keep checked content out of fatal(..)
messages too. Its toolExecutionRequest() gives the call that the failure stopped, with its id, so you can tell which
of several calls of one tool it was: for a ToolInputGuardrailException, with the arguments that the failing
guardrail validated (after the rewrites of the guardrails before it); for a ToolOutputGuardrailException, with the
arguments the tool was executed with.
A fatal failure ends the invocation and leaves the tool calls of that response of the LLM without results in the
chat memory. Many LLM providers (OpenAI, Anthropic and Amazon Bedrock among them) reject a
request whose conversation has a tool call without a result, so the next invocation with that chat memory can fail
too, until you remove that message from the memory or clear it. For content that the LLM should simply skip, prefer
failure(..) with a message such as "The result was blocked. Do not retry.", and keep fatal(..) for the cases
where the whole invocation must stop.
No retry and no reprompt
There is no counterpart of retry or reprompt of output guardrails,
and no maxRetries. The failure message already is the feedback to the LLM: it decides what to do next. A model
that keeps calling a tool will be stopped by maxToolCallingRoundTrips (default: 100).
Declaring tool guardrails
There are four ways to declare tool guardrails.
One guardrail instance serves every tool it applies to and every invocation of the AI Service, and it can run
concurrently, for example when tools run in parallel with
executeToolsConcurrently() or when several invocations run at
the same time. Make your guardrails thread-safe, and keep the state of one call in local variables, not in fields.
AiServices builder
Guardrails set on the builder apply to every tool of the AI Service, including tools that have no method (MCP
and ToolProvider tools, programmatic tools, and the built-in tool search tool).
There are instance and class variants, like for
input guardrails.
Here is a complete example, with the AllowedHostsInputGuardrail and the InjectionOutputGuardrail shown above:
class WebTools {
@Tool(name = "fetch_page", value = "Fetches a web page")
String fetchPage(@P(name = "url", value = "The URL of the page") String url) {
return "<html>...</html>"; // fetch the page here
}
@Tool("Searches the web")
String search(@P(name = "query", value = "What to search for") String query) {
return "1. ..."; // search the web here
}
}
interface Assistant {
String chat(String userMessage);
}
ChatModel chatModel = OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName(GPT_4_O_MINI)
.build();
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.tools(new WebTools())
.toolInputGuardrails(new AllowedHostsInputGuardrail())
.toolOutputGuardrailClasses(InjectionOutputGuardrail.class)
.build();
String answer = assistant.chat("Summarize the page https://example.com/news");
If the LLM calls fetch_page with this URL, AllowedHostsInputGuardrail rejects the call: the tool does not run, and
the LLM gets the failure message as the result of the tool. InjectionOutputGuardrail checks what both tools
return.
@P(name = "url") gives the parameter a fixed name. Without it, the name comes from the compiled class, and it is
arg0 unless the code is compiled with the -parameters option of javac. A guardrail that reads
argumentsAsMap().get("url") needs that name.
The methods are toolInputGuardrails(...), toolInputGuardrailClasses(...), toolOutputGuardrails(...),
toolOutputGuardrailClasses(...), and toolInputGuardrailsConfig(...) / toolOutputGuardrailsConfig(...).
Classes are instantiated with their default constructor, or by the
ClassInstanceFactory of your framework. Call these methods before
build(): after it, they throw an IllegalStateException. To build another AI Service with other tool guardrails,
start from a new builder.
Annotations
@ToolInputGuardrails
and
@ToolOutputGuardrails
can be placed on a tool method or on the class that declares the tools. They are an alternative to the
builder methods. Here is the WebTools class of the example above, with the same guardrails declared by annotations:
@ToolOutputGuardrails(InjectionOutputGuardrail.class) // every tool of the class
class WebTools {
@Tool(name = "fetch_page", value = "Fetches a web page")
@ToolInputGuardrails(AllowedHostsInputGuardrail.class) // this tool only
String fetchPage(@P(name = "url", value = "The URL of the page") String url) {
return "<html>...</html>"; // fetch the page here
}
@Tool("Searches the web")
String search(@P(name = "query", value = "What to search for") String query) {
return "1. ..."; // search the web here
}
}
The AI Service is then built without the tool guardrail methods of the builder:
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.tools(new WebTools())
.build();
Declare each guardrail in only one of the two ways: an instance given to the builder and an annotation that names its class both run (see Duplicates).
These annotations are unrelated to @InputGuardrails and @OutputGuardrails. @InputGuardrails applies to the user
message of an AI Service method, @ToolInputGuardrails applies to the arguments of a tool call. When a class is both
an AI Service and a tool (see
AI services as tools), each annotation keeps its own
meaning.
@ToolInputGuardrails and @ToolOutputGuardrails on an AI Service interface, or on its methods, do not guard the
tools of that AI Service. Set those guardrails on the AiServices builder (see above), or on the tool classes. They
only guard a method of the interface that is itself a tool. When such an annotation guards no tool, building the AI
Service fails with an IllegalConfigurationException that lists where the annotations are.
AiServiceTool
A tool that has no method can carry its guardrails (and its source) on its
AiServiceTool:
AiServiceTool tool = AiServiceTool.builder()
.toolSpecification(toolSpecification)
.toolExecutor(toolExecutor)
.toolInputGuardrails(new AllowedHostsInputGuardrail())
.toolOutputGuardrails(new InjectionOutputGuardrail())
.build();
This also works for the tools that a ToolProvider returns.
AgentBuilder
The builder of the agents of the langchain4j-agentic module has the setters
toolInputGuardrails(...), toolInputGuardrailClasses(...), toolOutputGuardrails(...) and
toolOutputGuardrailClasses(...), which take varargs (there are no List variants), plus the two
...GuardrailsConfig(...) setters. They are passed to the AiServices of the agent:
CreativeWriter writer = AgenticServices.agentBuilder(CreativeWriter.class)
.chatModel(chatModel)
.tools(new WebTools())
.toolOutputGuardrails(new InjectionOutputGuardrail())
.build();
Spring Boot
With the Spring Boot integration, the @ToolInputGuardrails and
@ToolOutputGuardrails annotations of your tool beans apply to the AI Services of your @AiService interfaces, also
when Spring proxies the beans. The @AiService annotation has no attributes for the guardrails that apply to every
tool: to set those, build the AI Service yourself with AiServices.builder(..) in a @Bean method.
Resolution order: the levels add up
This differs from AI service guardrails. For @InputGuardrails and @OutputGuardrails, the guardrails set on
the AiServices builder replace the annotations, and a method annotation replaces the class annotation. The
levels of tool guardrails never replace each other: every level contributes to the chain of a tool. Within the class
level and within the method level, the nearest declaration applies (see
Class hierarchies and overrides).
For each tool, the guardrails of all the levels form one chain, resolved separately for input and output. The levels nest around the tool, with the builder on the side of the LLM: its guardrails are the first to see the call of the LLM, and the last to see the result the LLM gets. The input guardrails run in this order:
- the guardrails set on the
AiServices(orAgentBuilder) builder - the guardrails of the
@ToolInputGuardrailsannotation on the class - the guardrails of the annotation on the method
- the guardrails of the tool's
AiServiceTool
The output guardrails run the levels in the reverse order:
- the guardrails of the tool's
AiServiceTool - the guardrails of the
@ToolOutputGuardrailsannotation on the method - the guardrails of the annotation on the class
- the guardrails set on the
AiServices(orAgentBuilder) builder
So the guardrails of the builder, which belong to your application, check the result as all the other guardrails
left it, including what the guardrails of a tool library or of a ToolProvider added to it. Some text is still
added after them, and no guardrail checks it: the note about changed
arguments, the failure messages of the guardrails, which replace the result once the whole chain has run, and the
text that replaces the result when the call is rolled back. A guardrail of a tool, written by whoever
knows what its output holds, can redact it before a global check sends it to an external service or to another LLM.
A global guardrail cannot redact the output before the guardrails of the tools run, and a global fatal(..) on
output runs after them: to redact first, put the redacting guardrail on the tool.
Within a level, the guardrails run in the order they are given, in both directions. Within the builder level, the
guardrail instances (toolInputGuardrails(...), toolOutputGuardrails(...)) run before the guardrails created from
classes (toolInputGuardrailClasses(...), toolOutputGuardrailClasses(...)), whatever order the setters were called
in.
Why not replace? A global injection check on the builder and a tool-specific check (a size limit on one tool) must
both run, and MCP and ToolProvider tools can only be guarded from the builder or their AiServiceTool: with
replace semantics, every per-tool guardrail would silently stop running as soon as you add a global one.
Class hierarchies and overrides
Within the class level and within the method level, the nearest declaration wins:
- Class level: the annotation of the class of the tool, else of its nearest superclass, else of its interfaces.
The class of the tool is the class of the object it runs on, also for a tool method that this class only inherits.
For a tool registered with
tools(Map)ortools(List<AiServiceTool>)and backed by aDefaultToolExecutor, the class is the runtime class of the object the tool runs on, so a proxy that subclasses your class (a CGLIB proxy, for example) reaches the annotations of your class. A JDK dynamic proxy, which only implements the interfaces, uses the class that declares the tool method. - Method level: the annotation of the tool method of that class, else of the method it overrides in a superclass,
else in an interface.
@Toolcan be on any of them. An overload with the same name in a superclass is not part of the chain.
So a subclass replaces the guardrails of its parent, and an annotation without guardrails removes them, also for the
tools that the subclass inherits without redeclaring them. This differs from the class-level annotations of Spring
(@Transactional) and Jakarta EE (@RolesAllowed), which do not apply to the methods a class inherits. When a nearer
annotation hides one further up, a DEBUG message is logged.
public interface OrderTools {
@Tool("Cancels an order")
String cancelOrder(String orderId);
}
public class DefaultOrderTools implements OrderTools {
@Override
@ToolInputGuardrails(OrderOwnershipInputGuardrail.class) // applies, although @Tool is on the interface
public String cancelOrder(String orderId) {
// ... cancel the order here
return "Order " + orderId + " canceled";
}
}
@ToolInputGuardrails({}) // removes the class guardrails of AuditedTools for every tool of this subclass, inherited ones included
public class TrustedTools extends AuditedTools {
}
Skipping some tools
Tool guardrails have no per-tool opt-out. A guardrail that was declared globally, or on a class, and must not apply to
some tools, skips them itself, using toolName() or toolSource().
For a security check, check every tool by default, and skip only the tools that you list as trusted. A guardrail that checks only the tools it knows about would silently skip any new tool, or any MCP server that you add later:
public class ExternalContentOutputGuardrail implements ToolOutputGuardrail {
// the MCP servers you trust, by the key of their McpClient
private static final Set<String> TRUSTED_SERVERS = Set.of("internal-docs");
// local tools that never return external content
private static final Set<String> SAFE_LOCAL_TOOLS = Set.of("clock");
@Override
public ToolOutputGuardrailResult validate(ToolOutputGuardrailRequest request) {
if (isTrusted(request)) {
return success();
}
// ... inspect request.text()
return success();
}
private static boolean isTrusted(ToolOutputGuardrailRequest request) {
if (request.toolSource() instanceof ToolSource.Mcp mcp) {
// skip only if the keys are known and all of them are trusted
return !mcp.serverKeys().isEmpty() && TRUSTED_SERVERS.containsAll(mcp.serverKeys());
}
return request.toolSource() instanceof ToolSource.Local
&& SAFE_LOCAL_TOOLS.contains(request.toolName());
}
}
serverKeys() holds only the keys that are known: a client without a key adds none. So a trust decision must
check that there is at least one key and that all of them are trusted. involves("internal-docs") alone is not
enough: it is also true for a tool whose source spans a trusted and an untrusted server. A DefaultMcpClient
built without key(...) gets a random key, which is never in your list, so its tools are checked (see
Guarding MCP tools).
The key is the only thing that tells the servers apart, and LangChain4j does not check that the keys of different clients differ. For a trust decision based on keys, give each server its own key, chosen by your application (never taken from the server or from user input): a client of an untrusted server that has the key of a trusted one is trusted too.
Duplicates
The same guardrail is not executed twice for one tool:
- The same guardrail class, declared by annotations or by the
...GuardrailClasses(...)setters at one or several levels, runs once. When the builder names it, it runs at the position of the builder: first for input, last for output. Otherwise it runs at the first position where it appears. Guardrails created from a class are all configured the same way, so running one of them is enough. - Instances are only duplicates of themselves: the same object passed twice runs once. Two instances of the same class run both, because they may be configured differently (for instance two regular-expression guardrails with different patterns), and silently dropping a check is worse than running one twice.
- An instance on the builder and an annotation that names the same class both run: LangChain4j cannot tell whether the instance is configured like the one it would create.
Telling the tools apart: ToolSource
A global guardrail sees all the tools, so it needs a way to tell them apart. Both requests have a
toolSource(),
which LangChain4j sets to one of:
ToolSource | Meaning |
|---|---|
ToolSource.Local | A local @Tool method: declaringClassName(), methodName() and parameterTypeNames(). Also any tool backed by a DefaultToolExecutor, however it is registered, such as a proxied bean that a framework registers through tools(Map): the source then names the original method, of your class, not of the proxy. |
ToolSource.Mcp | One or more MCP servers: serverKeys() and involves(String serverKey). See Guarding MCP tools. |
ToolSource.Programmatic | A tool registered programmatically, without an explicit source and without a DefaultToolExecutor (a lambda or another ToolExecutor). This is also the default for such tools of a custom ToolProvider. |
The source is trusted information. It is set by LangChain4j or by your application when the tool is registered, and it is never derived from remote data such as the tool specification's metadata or the hints an MCP server sends. A guardrail can therefore base security decisions on it.
All programmatic tools have the same source, ToolSource.programmatic(), which is the default for a tool that has
no DefaultToolExecutor, so a guardrail tells them apart by toolName(). An explicit toolSource(...) always
wins. A ToolSource.Local, ToolSource.Mcp or ToolSource.Programmatic can only be created with
ToolSource.local(...), ToolSource.mcp(...) or ToolSource.programmatic(). When a tool of a custom ToolProvider
or of tools(List<AiServiceTool>) runs a local method or calls an MCP server, you can give it the matching source
with toolSource(...) on its AiServiceTool.builder().
Later versions of LangChain4j may add kinds of sources (for example for A2A agents, or OpenAPI tools). A guardrail
should handle a kind it does not know, and a security check should treat it as untrusted, as the example in
Skipping some tools does: it trusts only the kinds and the tools it lists. Your application can
also define a kind of its own, and set it with toolSource(...). Make it immutable, with value-based equals and
hashCode and a toString, as a record does:
record A2aToolSource(String agentUrl) implements ToolSource {}
AiServiceTool tool = AiServiceTool.builder()
.toolSpecification(toolSpecification)
.toolExecutor(toolExecutor)
.toolSource(new A2aToolSource("https://agents.example.com/weather"))
.build();
A guardrail then recognizes it with request.toolSource() instanceof A2aToolSource a2a.
Which tools are guarded
Tool guardrails run for every tool that an AI Service, or an AI agent of the langchain4j-agentic
module, calls for the LLM. This includes the built-in tool search tool of a
ToolSearchStrategy and the tools it finds. The result of the tool search tool only
lists the names of the tools it found. The specifications of those tools, with their descriptions, are then added to
the tools the LLM sees in its next request, and tool guardrails do not check them. Tool descriptions are a known
injection vector ("tool poisoning"): for MCP tools, check them with the filter(..) or toolSpecificationMapper(..)
of the McpToolProvider builder (see Guarding MCP tools). An output guardrail
that rewrites the result of the tool search tool does not change which tools are added. Only an error result adds no
tools: a failure(..), or a rewrite to an error result.
Some built-in tools return instructions for the LLM on purpose: the tools of Skills
(activate_skill and read_skill_resource by default) return the instructions of a skill. A guardrail that looks for
instructions in tool results should skip them by name (request.toolName()). A guardrail that should not check the
tool search tool skips it the same way: its name is tool_search_tool by default, and the builders of
SimpleToolSearchStrategy, VectorToolSearchStrategy and DecisionModelToolSearchStrategy can change it with
toolName(..).
Tool guardrails do not run for:
- tools that the LLM provider runs on its own side (for example the web search and web fetch server tools of Anthropic, the built-in web search of OpenAI, or Google Search grounding in Gemini): their results reach the LLM at the provider, before LangChain4j sees them;
- MCP tool agents (
McpAgent,@McpClientAgent): they call their MCP tool directly, without an LLM tool call. To check what such an agent returns, add an input guardrail to the AI agent that reads its output.
Compensation
When compensation is enabled (compensateOnToolErrors(true)), "an
error result that the LLM sees means a failed step" applies to guardrails too. The rule depends on the final
result the LLM ends up with, not on which guardrail method was called, so guardrails need no special case:
| What happens | Failed step? | What is rolled back | CompensationReason |
|---|---|---|---|
An input guardrail rejects the call (failure(..)) | Yes | The other compensable tools. The rejected tool never ran, so there is nothing to roll back for it. | TOOL_INPUT_GUARDRAIL_FAILED |
An output guardrail turns a successful result into an error result (failure(..), or a rewrite with isError == true) | Yes | All compensable tools, including the rejected one, which stays compensable with its original result (the one before the guardrail). | TOOL_OUTPUT_GUARDRAIL_FAILED |
An output guardrail rewrites the result and keeps isError == false (successWith(String)) | No | Nothing. This is the way to sanitize a result without rolling anything back. | - |
The tool returned an error result and an output guardrail keeps it an error (successWith(String) keeps the flag) | Yes | Like any tool failure: the other compensable tools; the tool itself did not succeed so it is not compensable. | TOOL_EXECUTION_FAILED |
An output guardrail turns an error result into a success (successWith(contents, false)) | No | Nothing, and the tool is not compensable because it did not succeed. | - |
| A guardrail returns fatal | Like a thrown tool error | Like a tool error that ends the call, with the rules of the input and output rows above: the compensable tools are rolled back, then the invocation fails. See the note below. | TOOL_INPUT_GUARDRAIL_FAILED or TOOL_OUTPUT_GUARDRAIL_FAILED |
- The LLM sees the message of the rejecting guardrail for the rejected tool verbatim, not the "rolled back" text (after an input rewrite, with the note in front of it). The other tools that were rolled back get the usual rolled-back text.
- How far a failure reaches: every compensable tool of the whole invocation is rolled back, not only the ones of the current tool-calling round. With cross-agent compensation it also includes the compensable tools that other agents ran earlier.
- If the only goal of a guardrail on a read-only tool is to sanitize the result, use
successWith(..): a rejection withfailure(..)rolls back the writes that happened earlier in the invocation. - When a chain has several guardrails, only the final result counts.
- If a round has several failures, the first failing tool in request order decides the reason.
- A compensating action always gets the result that the tool returned, before its output guardrails, also when a
guardrail sanitized that result with
successWith(..)and the tool is rolled back later because of another tool.
A fatal guardrail failure is treated like a tool error that ends the call, in every AI Service mode (a plain return
value, TokenStream, CompletableFuture and Flow.Publisher): the compensable tools are rolled back first, and then
the invocation fails with the guardrail's exception. If a compensating action fails, its exception is added as a
suppressed exception to the guardrail's exception (see
Compensating Tool Actions).
Security notes
- Never quote the checked content in a failure message. A message such as
Blocked suspicious instruction: "<snippet>"sends the injection to the LLM anyway, because the message is sent to the LLM verbatim. - A guardrail that uses an LLM to judge content can be injected itself. Pass the tool result to the judge as data (clearly delimited, never as instructions), or combine the judge with a check that is not an LLM.
- A rejected output usually comes back identical on retry (the same URL gives the same page). The LLM may call
the tool again, up to
maxToolCallingRoundTripstimes (default: 100), and each run of an LLM-based guardrail is a paid call. Write the failure message so that it tells the LLM not to retry, or usefatal(...)when the whole invocation must stop (see the warning in Fatal failures). LowermaxToolCallingRoundTripsto bound the cost. text()only covers the text contents. Images and other non-text contents of a tool result pass through a guardrail that only checkstext(), andsuccessWith(String)keeps them. For untrusted sources, drop or check them withsuccessWith(List<Content>, boolean)(see Multi-content results).- Some texts that reach the LLM are not guarded. Tool guardrails do not see:
- the tool names, descriptions and parameter schemas that the LLM gets with the list of tools, for example from an MCP server, also for the tools that a tool search found (see Which tools are guarded);
- the results of tools that the LLM provider runs on its own side (see Which tools are guarded);
- what an MCP tool agent returns (see Which tools are guarded);
- the result for a tool name that the LLM made up (see Handling Tool Name Errors);
- the text that a
ToolArgumentsErrorHandlerproduces when an input guardrail cannot parse the arguments (see Arguments that cannot be parsed); - the note about changed arguments and the text that replaces a result that is rolled back, which are added after the output guardrails ran (see Resolution order).
- Keep secrets out of the arguments. The note about a rewrite shows the new values of the arguments the LLM sent,
and the LLM decides which arguments it sends: if it sends the key that a guardrail sets (because of a prompt
injection, for example), the note shows the value of the guardrail. Pass values that the LLM must not see in
InvocationParameters, or leave{{arguments}}out of the template of the note, or turn it off (see What the LLM is told about a rewrite). - Events carry the data before and after a rewrite. The tool guardrail events carry the arguments and the results
before a rewrite, and the events with the requests and the responses of the LLM carry the tool calls as the LLM made
them. The tool execution events,
toolExecutions()andafterToolExecutioncarry the arguments after a rewrite, with the values that a guardrail put in them, such as a token. Do not export them unredacted to an external system (see the warning in Observability). - A tool guardrail is not a sandbox. It checks what you tell it to check. Combine it with the least privilege principle for the tools themselves.
- Tool arguments are untrusted. Use
InvocationParametersor the chat memory id, not an argument the LLM supplies, to decide whose data a tool reads.
Observability
Each tool guardrail that returns a result fires an event, which you can listen to with a listener, like for
AI Service guardrails. A guardrail that throws, or whose validateAsync future fails,
fires no event.
| Event | Listener |
|---|---|
ToolInputGuardrailExecutedEvent | ToolInputGuardrailExecutedListener |
ToolOutputGuardrailExecutedEvent | ToolOutputGuardrailExecutedListener |
One event is fired for each guardrail that returned a result, with its request, its result and the duration. They are distinct from
InputGuardrailExecutedEvent and OutputGuardrailExecutedEvent: a listener for AI Service guardrails is not
notified of tool guardrails. See Observability for how to register listeners. A listener
of these events runs on the thread that completes the guardrail: in the CompletableFuture and reactive modes, that
can be a thread of the guardrail (see Non-blocking and reactive modes).
The request of a tool guardrail event holds what that guardrail checked: the arguments before an input rewrite,
the result before an output rewrite (in a chain, each guardrail's event holds the values that reached it).
ToolExecutedEvent and ToolCompensatedEvent carry the guarded values, with the arguments after an input rewrite,
including the values that a guardrail put in them (a token, for example). Other events carry the arguments as the
LLM sent them, before an input rewrite:
- the
AiServiceResponseReceivedEventof the LLM response with the tool call; - the
AiServiceRequestIssuedEventof every later request to the LLM, in this invocation and, with a chat memory, in the later ones: the conversation sent to the LLM keeps the tool call as the LLM made it; - the
AiServiceInvocationSummaryEvent, which holds all the events of the invocation.
A listener that exports events to an external system (logs, telemetry, an LLM observability platform) should drop or redact the request of the tool guardrail events, or not listen to them, and redact the tool calls in the requests and the responses of the LLM.
The tool execution events carry the request as the tool was executed with it, so after an input rewrite they carry the rewritten request. They also carry the guarded result, the one the LLM sees:
ToolExecutedEventcarries the guarded result.ToolCompensatedEvent(and theToolCompensatedEventof aFlow.Publisherstream) carries the guarded result from before the rollback replaced it, also for a tool whose result a guardrail sanitized and that was rolled back later, and the error result for a tool whose own output guardrail turned it into an error.- None of these events contains the note that the LLM gets with the result after an input rewrite.
- The compensating action itself gets the result that the tool actually returned, before its
output guardrails, since a guardrail may redact what the undo needs (a booking id, for example). The raw result is
also in the
ToolOutputGuardrailExecutedEvent.
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(chatModel)
.tools(new WebTools())
.toolOutputGuardrails(new InjectionOutputGuardrail())
.registerListener((ToolOutputGuardrailExecutedListener) event ->
log.info("{} on {}: {}",
event.guardrailClass().getSimpleName(),
event.request().toolName(),
event.result().result()))
.build();
Non-blocking and reactive modes
In the CompletableFuture and reactive modes, a tool guardrail must implement validateAsync like any
other guardrail (see Non-blocking and Reactive). A guardrail that does no blocking work
can call validate(...) directly. Like every asynchronous method, it reports a failure through the returned future
instead of throwing:
@Override
public CompletableFuture<ToolOutputGuardrailResult> validateAsync(ToolOutputGuardrailRequest request) {
try {
return CompletableFuture.completedFuture(validate(request));
} catch (Throwable e) {
return CompletableFuture.failedFuture(e);
}
}
validateAsync can be called on an I/O thread, for example the thread on which an MCP client completes a tool call.
It must not block: a guardrail that has blocking work to do runs it on an executor of its own and returns the future
of that work.
A guardrail does not decide which thread the rest of your code runs on. When the future of an input guardrail
completes on another thread, the tool is started on the executor of the tools. When the guardrails of a call complete
on another thread than the tool (an output guardrail that calls a moderation service over HTTP, or an input guardrail
that rejects the call), the rest of the round moves back to the executor of the tools, with the
context captured when the invocation started: the
ToolArgumentsErrorHandler, the afterToolExecution callback, the listeners of the tool events, the
rollback and the next call of the model. If the executor rejects that task (a full queue, a
shutdown), the rest of the round runs on the thread of the guardrail. A tool that completes on a thread of its own (an
asynchronous ToolExecutor) keeps that thread, as without guardrails. The listeners of the tool guardrail events
(ToolInputGuardrailExecutedListener, ToolOutputGuardrailExecutedListener) are the exception: they run while the
chain of guardrails runs, on the thread that completes the guardrail, without the captured context.
A guardrail that does not implement validateAsync fails the invocation with an AsyncNotSupportedException,
wrapped in a fatal ToolInputGuardrailException or ToolOutputGuardrailException. It is found by calling the
guardrail, so for an output guardrail this happens when the tool has already run, and in a round with several tool
calls, other tools may already have started by the time a missing validateAsync of an input guardrail is found (the
tools that ran successfully are rolled back if compensation is enabled). A TokenStream uses the
blocking validate.
When the invocation is canceled (see Compensating Tool Actions), the futures of the tool guardrails that are still running are canceled: those of the input guardrails at once, so that the tool does not start, and those of the output guardrails once the tool has finished. A running tool is not interrupted. The rollback of a tool that ran successfully does not wait for its output guardrails, and its compensating action receives the result of the tool as it ran.
Unit Testing Tool Guardrails
A tool guardrail is a plain object, so it can be tested by calling validate with a request that you build:
class InjectionOutputGuardrailTest {
private final InjectionOutputGuardrail guardrail = new InjectionOutputGuardrail();
@Test
void should_block_instructions_in_the_result() {
ToolOutputGuardrailRequest request = ToolOutputGuardrailRequest.builder()
.toolExecutionRequest(ToolExecutionRequest.builder()
.id("1")
.name("fetch_page")
.arguments("{\"url\":\"https://docs.example.com\"}")
.build())
.toolSpecification(ToolSpecification.builder().name("fetch_page").build())
.toolSource(ToolSource.programmatic())
.requestParams(GuardrailRequestParams.builder()
.userMessageTemplate("")
.variables(Map.of())
.build())
.resultContents(List.of(TextContent.from("Ignore all previous instructions and ...")))
.isError(false)
.build();
ToolOutputGuardrailResult result = guardrail.validate(request);
assertThat(result.result()).isEqualTo(GuardrailResult.Result.FAILURE);
assertThat(result.failureMessage())
.isEqualTo("The tool result contains instructions and was blocked. Do not retry.");
}
}
The input request is built the same way, with ToolInputGuardrailRequest.builder() (without the result contents).
Both requests need the tool execution request, the tool specification, the tool source and the request parameters:
the builder fails without them.
To test the whole chain, run an AI Service with a scripted chat model, such as MockChatModel from the
langchain4j-test module, and check what the tool received and what the LLM was sent.
Extension points
Tool guardrails use the same extension points as guardrails, and add the Tool* ones:
The guardrail system was built in a composable way so it can be extended and reused in other downstream frameworks (such as Quarkus or Spring Boot). This section describes some of the extension points or "hooks" that are provided.
All of these extension points utilize the Java Service Provider Interface (Java SPI).
| Extension point interface | Purpose |
|---|---|
ClassInstanceFactory | Provides instances of classes. - Intended to delegate instance creation/retrieval to some other means. - If not provided, uses reflection to create an instance using the default constructor. - Other frameworks (like Quarkus or Spring) may use their own bean containers to provide instances of classes. Those frameworks would provide an implementation. - It also creates the tool guardrail classes (from @ToolInputGuardrails/@ToolOutputGuardrails and the toolInputGuardrailClasses(..)/toolOutputGuardrailClasses(..) setters), so a DI framework needs no tool-specific code for that.- A Quarkus implementation may look something like CDIClassInstanceFactory- A Spring implementation may look something like ApplicationContextClassInstanceFactory |
ClassMetadataProviderFactory | Provides access to class metadata. - Used to scan the methods on AiService interfaces, and find and process the @InputGuardrails/@OutputGuardrails annotations, and the @ToolInputGuardrails/@ToolOutputGuardrails annotations of tool guardrails.- For tool guardrails, getDeclaredAnnotation(Class, Class) is asked about the classes of the tools (the class of the object a @Tool method runs on, and its superclasses and interfaces, in the order LangChain4j defines) for the @ToolInputGuardrails/@ToolOutputGuardrails annotations. It must return only an annotation declared on the class itself. Its default implementation returns getAnnotation(Class, Class), so a factory whose getAnnotation(Class, Class) also searches superclasses or interfaces (as Spring's AnnotationUtils.findAnnotation does) must override it. The annotations of the tool methods are read from the Method. A factory must return an empty Optional for an annotation or a class it has no metadata for, and must not throw.- ReflectionBasedClassMetadataProviderFactory is the default implementation if no others are found, providing class metadata using reflection. |
GuardrailServiceBuilderFactory | Provides builder instances for building GuardrailService instances. An application or framework would implement this if they needed to customize the way they build GuardrailService instances. |
InputGuardrailsConfigBuilderFactory | - SPI for overriding and/or extending the default InputGuardrailsConfigBuilder- Other frameworks may provide their own implementation with extra additional configuration for input guardrails. - Would also allow other frameworks to drive input guardrail configuration via some other mechanism (i.e. a properties file). |
OutputGuardrailsConfigBuilderFactory | - SPI for overriding and/or extending the default OutputGuardrailsConfigBuilder- Other frameworks may provide their own implementation with extra additional configuration for output guardrails. - Would also allow other frameworks to drive output guardrail configuration via some other mechanism (i.e. a properties file). |
InputGuardrailExecutorBuilderFactory | - SPI for overriding and/or extending the default InputGuardrailExecutorBuilder responsible for building InputGuardrailExecutor instances. |
OutputGuardrailExecutorBuilderFactory | - SPI for overriding and/or extending the default OutputGuardrailExecutorBuilder responsible for building OutputGuardrailExecutor instances. |
ToolGuardrailServiceBuilderFactory | Provides builder instances for building ToolGuardrailService instances, which resolve and run the tool guardrails of each tool. The counterpart of GuardrailServiceBuilderFactory for tool guardrails. |
ToolInputGuardrailExecutorBuilderFactory | SPI for overriding and/or extending the default builder responsible for building ToolInputGuardrailExecutor instances. |
ToolOutputGuardrailExecutorBuilderFactory | SPI for overriding and/or extending the default builder responsible for building ToolOutputGuardrailExecutor instances. |
ToolInputGuardrailsConfigBuilderFactory | SPI for overriding and/or extending the default ToolInputGuardrailsConfig builder. Other frameworks may provide their own implementation to drive tool input guardrail configuration via some other mechanism (i.e. a properties file). |
ToolOutputGuardrailsConfigBuilderFactory | SPI for overriding and/or extending the default ToolOutputGuardrailsConfig builder. Other frameworks may provide their own implementation to drive tool output guardrail configuration via some other mechanism (i.e. a properties file). |
To customize guardrails, a framework provides a ClassMetadataProviderFactory and a ClassInstanceFactory and uses the default guardrail builders and services. It replaces the whole builder through GuardrailServiceBuilderFactory (or ToolGuardrailServiceBuilderFactory) only if it must.
A framework that runs its own tool loop can reuse the tool guardrails through ToolGuardrailService: it builds one with ToolGuardrailService.builder(aiServiceClass) (for the guardrails that apply to every tool), and binds each tool with bind(aiServiceTool), where aiServiceTool is the AiServiceTool of the tool and carries the guardrails that the framework found for the tool. It runs each tool call with executeGuardrails(key, request, requestParams, executeTool), where executeTool is a Function<ToolExecutionRequest, ToolExecutionResult> that runs the tool with the request it is given (the call as the input guardrails left it). The GuardedToolExecution it returns holds that request and the result for the LLM. Before the result goes to the LLM, the framework passes its contents through resultContentsForLlm(..) of the GuardedToolExecution, which adds the note about changed arguments (see What the LLM is told about a rewrite), and it does the same with the text that replaces the result when the call is rolled back (resultContentsForLlm(List.of(TextContent.from(text)))). A service that overrides executeGuardrails(..) adjusts the execution of the default implementation with toBuilder(), which keeps the note. See Running the tool guardrails in your own tool loop for an example. The key that bind(..) returns carries the specification and the source of the tool, and only works with the service that returned it. A framework that builds the guardrail requests itself and calls executeInputGuardrails(key, request) / executeOutputGuardrails(key, request) (or their Async variants) builds them from the key: a request whose tool name, specification or source differs from the key's fails with an IllegalArgumentException (the Async variants fail their future), so a tool is never reported with another tool's specification or as less trusted than it is. A framework that builds its own ToolServiceContext for the tool loop of LangChain4j sets the service with toolGuardrailService(..) and the keys that bind(..) returned with toolGuardrailKeys(..). A tool of the context without a key is bound on demand when it is called, so it gets at least the guardrails that apply to every tool. A context built without a tool guardrail service uses the tool guardrail service of the AI Service, and binds its tools on demand the same way, so the tool guardrails of the AI Service are never skipped.
Running the tool guardrails in your own tool loop
A framework that runs the tool calls of the LLM itself, instead of letting an AI Service run them, can still run the
tool guardrails, with a ToolGuardrailService:
// once: the guardrails that apply to every tool
ToolGuardrailService guardrailService = ToolGuardrailService.builder(Assistant.class)
.outputGuardrails(new InjectionOutputGuardrail())
.build();
// once per tool: the guardrails that the framework found for this tool go on its AiServiceTool
ToolKey key = guardrailService.bind(AiServiceTool.builder()
.toolSpecification(toolSpecification)
.toolExecutor(toolExecutor)
.toolInputGuardrails(new AllowedHostsInputGuardrail())
.build());
// for each tool call of the LLM
GuardedToolExecution execution = guardrailService.executeGuardrails(
key,
toolExecutionRequest,
requestParams,
effectiveRequest -> toolExecutor.executeWithContext(effectiveRequest, invocationContext));
// resultContentsForLlm(..) adds the note about changed arguments, if there is one
ToolExecutionResultMessage resultMessage = ToolExecutionResultMessage.builder()
.id(toolExecutionRequest.id())
.toolName(toolExecutionRequest.name())
.contents(execution.resultContentsForLlm(execution.result().resultContents()))
.isError(execution.result().isError())
.attributes(execution.result().attributes())
.build();
// report the call, for example as a ToolExecution, without the note
ToolExecution toolExecution = ToolExecution.builder()
.request(execution.request())
.result(execution.result())
.invocationContext(invocationContext)
.resultReplaced(execution.isResultReplaced())
.build();
ToolKeyisToolGuardrailService.ToolKey. A key only works with the service that returned it. Bind each tool once, and bind again a tool that is provided anew for an invocation (by aToolProvider, for example).bind(..)reads the@ToolInputGuardrailsand@ToolOutputGuardrailsannotations only for a tool whose executor is aDefaultToolExecutor. A framework that runs a tool with an executor of its own resolves the annotations of the tool itself, and puts the guardrails it found on theAiServiceTool. They then run at the level of theAiServiceTool, in the order they are given: give the class guardrails before the method guardrails for input, and the method guardrails before the class guardrails for output, to keep the order of the annotations (see Resolution order). Guardrails on anAiServiceToolare instances, and an instance is only a duplicate of itself (see Duplicates): leave out of theAiServiceToolthe guardrails that the service builder already has, or they run twice.- Set the source of the tool with
toolSource(..)on theAiServiceToolbuilder, for exampleToolSource.local(..)for a method of the application. Without it, a tool whose executor is not aDefaultToolExecutorgetsToolSource.programmatic(). requestParamsare theGuardrailRequestParamsof the invocation, as for the AI Service guardrails.- The function given to
executeGuardrails(..)runs the tool with the request it gets: the call as the input guardrails left it.execution.request()holds that request. Report the call, in events for example, withexecution.request()andexecution.result(), which hold no note. execution.isResultReplaced()tells whether an output guardrail replaced the result of the tool, with a rewrite or with the message of afailure(..). Copy it to theToolExecutionthe framework reports, withresultReplaced(..)as above, so thatToolExecution#isResultReplaced()tells a replaced result apart from a tool that returnednull.- A fatal failure throws a
ToolInputGuardrailExceptionor aToolOutputGuardrailException, whosetoolExecutionRequest()gives the call it stopped.executeGuardrailsAsync(..)is the non-blocking counterpart: its function returns aCompletableFuture<ToolExecutionResult>, and every failure fails the future it returns. It does not switch threads: the function is called on the thread that completes the input guardrails (the calling thread when the tool has none, or when they complete at once), so start the tool on an executor of your own rather than run blocking work in the function, and the returned future completes on the thread that completes the tool or the last output guardrail. A framework that must go on with its own thread or context moves back to it itself. - When the framework rolls the call back later and replaces its result with a text, it passes that text through
execution.resultContentsForLlm(List.of(TextContent.from(text)))too. - A service that overrides
executeGuardrails(..)to decorate the execution of the default implementation adjusts it withexecution.toBuilder(), which keeps the note.
Differences from the Quarkus tool guardrails
The tool guardrails were originally developed in the Quarkus LangChain4j extension. The ones in LangChain4j differ in these points:
- The failure message is sent to the LLM verbatim, with no framework prefix (after an input rewrite, the note comes before the message of an output guardrail).
- Output guardrails also run on error results, for example when the tool throws and the error is sent to the LLM (see Output guardrails also see error results). The Quarkus extension skips its output guardrails when the tool throws.
- The chain continues after a non-fatal failure, and the failures are aggregated.
- The request types (
ToolInputGuardrailRequest,ToolOutputGuardrailRequest) andToolSourceare inlangchain4j-core, and a request hastoolSource(). - The Quarkus extension declares tool guardrails on tool methods only. In LangChain4j, they can also be declared on
the class of the tools, on the
AiServicesorAgentBuilderbuilder and on anAiServiceTool, and the guardrails of all the levels run, instead of replacing each other. - After an input guardrail changed the arguments, the LLM gets a note in the tool result (see What the LLM is told about a rewrite). The Quarkus extension sends the result of the tool unchanged.
- A rewrite returns only what changes: an input guardrail returns the new arguments with
successWith(String)(a JSON object) orsuccessWithArguments(Map), and an output guardrail returns the new text withsuccessWith(String), or the new contents withsuccessWith(List<Content>, boolean). In the Quarkus extension, a rewrite returns a whole newToolExecutionRequestorToolExecutionResult(successWith(ToolExecutionRequest),successWith(ToolExecutionResult)). - In the
CompletableFutureand reactive modes, tool guardrails run throughvalidateAsync, which must not block (see Non-blocking and reactive modes). The Quarkus extension does not run tool guardrails on the event loop: a guarded tool called there fails with aBlockingToolNotAllowedException, so a guarded tool must be blocking.
In a Quarkus application, use the Quarkus extension's own tool guardrails: the annotations and builder methods described on this page are not applied by the Quarkus extension yet (see quarkiverse/quarkus-langchain4j#3162).