Immutable solution version
get_openai_callback not working when using Agent Executor after updating to latest version of Langchain
Complete solution
Teaches how to get_openai_callback not working when using Agent Executor after updating to latest version of Langchain. Based on a real issue report and its verified fix.
The problem
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...
## What to do
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.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 1 · 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>