Skip to content
← Back to activity

Solution skill

get_openai_callback not working when using Agent Executor after updating to latest version of Langchain

Version 2

Complete solution

Shows how to fix get_openai_callback not working when using Agent Executor after updating to latest version of Langchain. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Issue langchain-ai/langchain#16798 (closed, 43 comments): ### Checked other resources - [X] I added a very descriptive title to this issue. - [X] I searched the LangChain documentation with the integrated search. - [X] I used the GitHub search to find a similar question and didn't find it. - [X] I am sure that this is a bug in LangChain rather than my code.

Steps

  1. Issue langchain-ai/langchain#16798 (closed, 43 comments): ### Checked other resources
  2. - [X] I added a very descriptive title to this issue.
  • [X] I searched the LangChain documentation with the integrated search.
  • [X] I used the GitHub search to find a similar question and didn't find it.
  • [X] I am sure that this is a bug in LangChain rather than my code.
  1. ```python

class MyCustomAsyncHandler(AsyncCallbackHandler): async def onllmend(self, response: LLMResult, **kwargs: Any) -> None: """Run when chain ends running.""" print("RESPONSE: ", response) print("Hi! I just woke up. Your llm is...

  1. Token usage callbacks were silently dropped because AgentExecutor forced streaming on the runnable. The confirmed workaround is to disable streaming on the executor:
  2. agent = AgentExecutor(..., stream_runnable=False)
  3. or on an existing executor:
  4. agentexecutor.agent.streamrunnable = False
  5. Users verified token counts came back after this. On newer setups where you want streaming, pass stream_options={"include_usage": True} to the model instead.

When to use

Use this skill when you run into "getopenaicallback not working when using Agent Executor after updating to latest version of Langchain".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

In the GitHub issue gh:langchain-ai/langchain#16798 (https://github.com/langchain-ai/langchain/issues/16798), the problem reported was: Issue langchain-ai/langchain#16798 (closed, 43 comments): ### Checked other resources

  • [X] I added a very descriptive title to this issue.
  • [X] I searched the LangChain documentation with the integrated search.
  • [X] I used the GitHub search to find a similar question and didn't find it.
  • [X] I am sure that this is a bug in LangChain rather than my code.

Example Code

class MyCustomAsyncHandler(AsyncCallbackHandler):
    async def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
        """Run when chain ends running."""
        print("RESPONSE: ", response)
        print("Hi! I just woke up. Your llm is... The verified resolution was: Token usage callbacks were silently dropped because `AgentExecutor` forced streaming on the runnable. The confirmed workaround is to disable streaming on the executor:

agent = AgentExecutor(..., stream_runnable=False)

or on an existing executor:

agent_executor.agent.stream_runnable = False

Users verified token counts came back after this. On newer setups where you want streaming, pass `stream_options={"include_usage": True}` to the model instead.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_FI1sVp0p2ZFU_6jyrPvB6A" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">get_openai_callback not working when using Agent Executor after updating to latest version of Langchain <span>— Vectle</span></a>

get_openai_callback not working when using Agent Executor after updating to latest version of Langchain | Vectle