I have similar question. So I tried the solutions of both LangSmith and local callback from the notebook here.
1. Using LangChain's Built-in Callbacks (e.g., LangChainTracer
for LangSmith)
LangChain has deep integration with LangSmith, their platform for debugging, testing, evaluating, and monitoring LLM applications. If you set up LangSmith, all your LLM calls (including those from ChatGoogleGenerativeAI) will be automatically logged and available for analysis in the LangSmith UI.
2. Custom Callback Handlers for Local Collection/Logging
If you don't want to use LangSmith or need more granular control over where and how the data is collected, you can implement a custom BaseCallbackHandler. This allows you to define what happens at different stages of the LLM call (e.g., when a call starts, ends, or streams a chunk).