{ "cells": [ { "cell_type": "markdown", "id": "562ddb82", "metadata": {}, "source": [ "# How to stream LLM tokens from your graph\n", "\n", "In this example, we will stream tokens from the language model powering an\n", "agent. We will use a ReAct agent as an example.\n", "\n", "
Note
\n", "\n",
" If you are using a version of @langchain/core
< 0.2.3, when calling chat models or LLMs you need to call await model.stream()
within your nodes to get token-by-token streaming events, and aggregate final outputs if needed to update the graph state. In later versions of @langchain/core
, this occurs automatically, and you can call await model.invoke()
.\n",
"
\n",
" For more on how to upgrade @langchain/core
, check out the instructions here.\n",
"
Streaming Support
\n", "\n", " Token streaming is supported by many, but not all chat models. Check to see if your LLM integration supports token streaming here (doc). Note that some integrations may support general token streaming but lack support for streaming tool calls.\n", "
\n", "Note
\n", "\n",
" In this how-to, we will create our agent from scratch to be transparent (but verbose). You can accomplish similar functionality using the createReactAgent({ llm, tools })
(API doc) constructor. This may be more appropriate if you are used to LangChain's AgentExecutor class.\n",
"
Note
\n", "\n", " These model requirements are not general requirements for using LangGraph - they are just requirements for this one example.\n", "
\n", "Compatibility
\n", "\n",
" This section requires @langchain/langgraph>=0.2.20
. For help upgrading, see this guide.\n",
"