Skip to content

chain not being updated on llm_token_usage callback? #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aaparmeggiani opened this issue Mar 17, 2025 · 6 comments
Open

chain not being updated on llm_token_usage callback? #267

aaparmeggiani opened this issue Mar 17, 2025 · 6 comments

Comments

@aaparmeggiani
Copy link
Contributor

Not sure if expected behaviour, but last_message.content has different values for the example below.
Shouldn't they be the same once usage values refer to both messages?

Thanks!

events = %{
  on_llm_token_usage: fn chain, _usage ->
    IO.inspect(chain.last_message.content, label: "FROM_CALLBACK" )
  end
}

{:ok, updated_chain} = LLMChain.new!( %{llm: ChatOpenAI.new!()} ) 
  |> LLMChain.add_message(Message.new_user!("What's your name?"))
  |> LLMChain.add_callback(events)
  |> LLMChain.run(mode: :while_needs_response)

IO.inspect(updated_chain.last_message.content, label: "FROM_RUN")
FROM_CALLBACK: "What's your name?"
FROM_RUN: "I am an AI assistant and my name is Assistant. How can I help you today?"
@aaparmeggiani aaparmeggiani changed the title updated_chain not being updated on llm_token_usage callback? chain not being updated on llm_token_usage callback? Mar 17, 2025
@brainlid
Copy link
Owner

brainlid commented Mar 17, 2025 via email

@aaparmeggiani
Copy link
Contributor Author

Thanks for your quick reply, Mark. Yes, that looks fine if you only need the usage metadata, but it becomes tricky if you also need access to the output itself—for instance, when feeding a trace.

@brainlid
Copy link
Owner

I looked deeper into it. There isn't a good way to combine the two.

The token usage data and the message are processed by the ChatOpenAI module. The chain is managed by the LLMChain and it doesn't get updated until after the ChatOpenAI module is done. So the last_message on the chain is not updated at that point.

The purpose for making the chain available to the callback was so the custom_context data and the llm model could be available for making decisions in callbacks.

If you need to tie the data in the token usage callback together with something of the request, you can perhaps put something in the custom_context that you access in the callback.

@aaparmeggiani
Copy link
Contributor Author

Cheers Mark!

@aaparmeggiani
Copy link
Contributor Author

Reopening for further thoughts. The issue with custom_context is that it needs to be set before running the chain.

The solution I've found so far is storing the usage directly in the message, as metadata, eliminating the need for a callback altogether in my use case. aaparmeggiani@3d750a7

@brainlid
Copy link
Owner

brainlid commented Apr 5, 2025

FYI, message's will include the usage information in Message.metadata.usage. It's being added to MessageDeltas as well and they can be accumulated together. This should provide a much clearer and more direct link of token usage with each request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants