Spring AI Recipe: Logging LLM Requests and Responses
When building applications with Spring AI, it’s incredibly useful to inspect the requests sent to the LLM and the responses returned.
This visibility helps with:
- debugging unexpected behavior
- understanding how tools are invoked
- observing how prompts and advisors influence results
This recipe explores two approaches:
- Using Spring AI’s built-in logging advisor
- Using Logbook for deeper inspection
NOTE: This recipe has been updated since it’s original publication to reflect changes in Spring AI 2.0.0. It’s also worth noting that a later recipe revises how
SimpleLoggerAdvisoris used.
Using SimpleLoggerAdvisor
Spring AI provides a built-in advisor for logging LLM interactions: SimpleLoggerAdvisor.
You can enable it using a ChatClientBuilderCustomizer:
@Bean
ChatClientBuilderCustomizer loggingCustomizer() {
return builder -> builder.defaultAdvisors(
SimpleLoggerAdvisor.builder().build()
);
}Because this advisor logs at the debug level, you must enable logging:
logging.level.org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor=debugWhat gets logged
When enabled, SimpleLoggerAdvisor logs:
- the request, represented as a
ChatClientRequest - the response, returned from the model
This provides a clear view of:
- the prompt sent to the model
- the final output
The limitation
While useful, SimpleLoggerAdvisor does not capture the full interaction when tools are involved.
Tool usage typically follows this pattern:
- The application sends a request to the LLM
- The LLM responds with a tool invocation request
- The application executes the tool
- The result is sent back to the LLM
- The LLM produces the final response
SimpleLoggerAdvisor only logs:
- the initial request
- the final response
The intermediate steps are not visible.
Using Logbook for full visibility
To capture the full interaction — including tool calls — you can use Logbook.
Add the dependency:
implementation 'org.zalando:logbook-spring-boot-starter:4.0.3'Configure a RestClientCustomizer:
@Bean
RestClientCustomizer logbookCustomizer(
LogbookClientHttpRequestInterceptor interceptor) {
return restClient -> restClient.requestInterceptor(interceptor);
}Enable logging at trace level:
logging.level.org.zalando=traceWhat Logbook provides
With Logbook enabled, you will see:
- every HTTP request to the model API
- every HTTP response from the model API
- all intermediate steps in tool execution
This includes:
- the LLM requesting tool execution
- the application sending tool results back
- the final response
This gives a complete picture of the interaction loop.
Important limitation
Logbook only works if the underlying model implementation uses Spring’s RestClient. Many of Spring AI’s model abstractions no longer use RestClient under the covers and instead rely on the API’s own Java libraries.
Takeaway
Both approaches are useful, depending on your needs:
SimpleLoggerAdvisor
- Easy to enable
- Good for high-level inspection
Logbook
- More detailed
- Captures full request/response cycles
- Ideal for debugging tool interactions
Final thoughts
As applications become more agentic — leveraging tools, memory, and multi-step reasoning — visibility into LLM interactions becomes increasingly important.
Logging is not just for debugging — it’s a key part of understanding and refining how your system behaves. Being able to see what requests and responses are sent between your application and an LLM puts a spotlight on what’s really going on, helping to understand how certain Spring AI features work as well as why the LLM responds the way it does.
You can find the source code for this and other Spring AI Recipes at https://github.com/habuma/spring-ai-recipes