Sitemap

Spring AI Recipe: Logging LLM Requests and Responses

3 min readApr 8, 2026

--

Press enter or click to view image in full size

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:

  1. Using Spring AI’s built-in logging advisor
  2. 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 SimpleLoggerAdvisor is 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=debug

What 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:

  1. The application sends a request to the LLM
  2. The LLM responds with a tool invocation request
  3. The application executes the tool
  4. The result is sent back to the LLM
  5. 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=trace

What 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

--

--

Craig Walls
Craig Walls

Written by Craig Walls

Author Spring AI in Action, Spring in Action, and Build Talking Apps for Alexa