<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe" -->

---
title: Spring AI 2.0: Custom Advisors for Tool & Token Logging
description: A walkthrough of building custom advisors in Spring AI 2.0 to gain visibility into LLM calls. Two advisors are created from scratch: one that logs which tools...
canonical: https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Spring AI 2.0: Custom Advisors for Tool & Token Logging | daily.dev
og:description: A walkthrough of building custom advisors in Spring AI 2.0 to gain visibility into LLM calls. Two advisors are created from scratch: one that logs which tools...
og:url: https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe
og:image: https://api.daily.dev/og/posts/JD23SdDWe.png
og:image:alt: Spring AI 2.0: Custom Advisors for Tool & Token Logging
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Spring AI 2.0: Custom Advisors for Tool & Token Logging

**[Dan Vega](https://daily.dev/sources/dan-vega)** · 32 min read · 1 upvotes · 0 comments

## Summary

A walkthrough of building custom advisors in Spring AI 2.0 to gain visibility into LLM calls. Two advisors are created from scratch: one that logs which tools are available to the model before each call, and another that tracks token consumption (prompt, completion, total) after each call. The tutorial covers implementing the BaseAdvisor interface, using the before/after lifecycle hooks, accessing ChatResponse metadata for usage stats, and ordering advisors in the chain. The context is a practical need to monitor tool loading and token costs when working with many tools in the context window.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=WNJFR-_N9bc>

## Questions this post answers

### How do I create a custom advisor in Spring AI to log which tools are available to the LLM before a call?

Implement the BaseAdvisor interface from the Spring AI ChatClient advisor API, which extends CallAdvisor and StreamAdvisor and requires before and after methods. In the before method, check if the request's ChatOptions is an instance of ToolCallingChatOptions and get its tool callbacks; map each callback to its tool definition name, sort them, and print the resulting list before returning the unchanged request.

_Developers wiring observability into LLM tool calls can track advisor patterns like this on daily.dev._

### How can I track prompt, completion, and total token usage across multiple Spring AI chat calls?

Build a custom advisor implementing BaseAdvisor whose after method reads the ChatResponse metadata's usage object, which contains prompt tokens, completion tokens, and total tokens for that call. Use AtomicInteger fields to accumulate running totals across calls (add and get for prompt, completion, and total), then log both the per-call and running totals before returning the response.

_Anyone tuning token spend across LLM calls can keep tabs on approaches like this via daily.dev._

### Why would loading many tools into an LLM context become a problem in Spring AI applications?

When tools are registered with a chat client, all of their definitions get loaded into the prompt sent to the LLM, consuming context window space proportional to the number of tools. Loading around 100 tools means every tool's definition is included on every call, which increases token usage even for tools the model never invokes, motivating a tool search approach to load tools more selectively.

_Teams scaling agent tool counts can watch for context-window tradeoffs like this on daily.dev._

## Similar posts on daily.dev

- [Spring AI : Advisors API](https://daily.dev/posts/spring-ai-advisors-api-ugswsrjzz) · SivaLabs · 4 upvotes · 0 comments
- [Spring AI’s Dynamic Tool Discovery](https://daily.dev/posts/spring-ai-s-dynamic-tool-discovery-k2apeohgr) · Baeldung · 0 upvotes · 0 comments
- [Smart Tool Selection: Achieving 34-64% Token Savings with Spring AI's Dynamic Tool Discovery](https://daily.dev/posts/smart-tool-selection-achieving-34-64-token-savings-with-spring-ai-s-dynamic-tool-discovery-n1bvjlpdj) · Spring · 2 upvotes · 0 comments

---

Tags: [#llm](https://daily.dev/tags/llm), [#java](https://daily.dev/tags/java), [#spring-boot](https://daily.dev/tags/spring-boot), [#spring-ai](https://daily.dev/tags/spring-ai)

[View this post on daily.dev](https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Spring AI 2.0: Custom Advisors for Tool & Token Logging","url":"https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe"},"datePublished":"2026-07-08T18:43:12.826Z","dateModified":"2026-09-14T09:03:38.858Z","description":"A walkthrough of building custom advisors in Spring AI 2.0 to gain visibility into LLM calls. Two advisors are created from scratch: one that logs which tools...","image":"https://i.ytimg.com/vi/WNJFR-_N9bc/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/WNJFR-_N9bc/sddefault.jpg","isAccessibleForFree":true,"articleSection":"Dan Vega","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Dan Vega","logo":"https://media.daily.dev/image/upload/s--qCQZxa91--/f_auto,q_auto/v1773648785/logos/dan-vega?_a=BAMAMiiu0","url":"https://daily.dev/sources/dan-vega"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"llm,java,spring-boot,spring-ai","timeRequired":"PT32M","video":{"@type":"VideoObject","name":"Spring AI 2.0: Custom Advisors for Tool & Token Logging","description":"A walkthrough of building custom advisors in Spring AI 2.0 to gain visibility into LLM calls. Two advisors are created from scratch: one that logs which tools...","thumbnailUrl":"https://i.ytimg.com/vi/WNJFR-_N9bc/sddefault.jpg","uploadDate":"2026-07-08T18:43:12.826Z","duration":"PT32M","url":"https://api.daily.dev/r/JD23SdDWe","embedUrl":"https://www.youtube.com/embed/WNJFR-_N9bc"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Dan Vega","item":"https://daily.dev/sources/dan-vega"},{"@type":"ListItem","position":3,"name":"Spring AI 2.0: Custom Advisors for Tool & Token Logging"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/spring-ai-2-0-custom-advisors-for-tool-token-logging-jd23sddwe#faq","mainEntity":[{"@type":"Question","name":"How do I create a custom advisor in Spring AI to log which tools are available to the LLM before a call?","acceptedAnswer":{"@type":"Answer","text":"Implement the BaseAdvisor interface from the Spring AI ChatClient advisor API, which extends CallAdvisor and StreamAdvisor and requires before and after methods. In the before method, check if the request's ChatOptions is an instance of ToolCallingChatOptions and get its tool callbacks; map each callback to its tool definition name, sort them, and print the resulting list before returning the unchanged request. Developers wiring observability into LLM tool calls can track advisor patterns like this on daily.dev."}},{"@type":"Question","name":"How can I track prompt, completion, and total token usage across multiple Spring AI chat calls?","acceptedAnswer":{"@type":"Answer","text":"Build a custom advisor implementing BaseAdvisor whose after method reads the ChatResponse metadata's usage object, which contains prompt tokens, completion tokens, and total tokens for that call. Use AtomicInteger fields to accumulate running totals across calls (add and get for prompt, completion, and total), then log both the per-call and running totals before returning the response. Anyone tuning token spend across LLM calls can keep tabs on approaches like this via daily.dev."}},{"@type":"Question","name":"Why would loading many tools into an LLM context become a problem in Spring AI applications?","acceptedAnswer":{"@type":"Answer","text":"When tools are registered with a chat client, all of their definitions get loaded into the prompt sent to the LLM, consuming context window space proportional to the number of tools. Loading around 100 tools means every tool's definition is included on every call, which increases token usage even for tools the model never invokes, motivating a tool search approach to load tools more selectively. Teams scaling agent tool counts can watch for context-window tradeoffs like this on daily.dev."}}]}
```

