LangChain Streaming Output | Online Tutorial
\\\\n\\\\nStreaming output makes AI responses appear character by character like typing, greatly improving user experience. LangChain's Agent has built-in complete streaming output support.
\\\\n\\\\n\\\\n\\\\n
Why Streaming Output is Needed
\\\\n\\\\nIf using invoke(), users need to wait for the Agent to complete all steps (multiple model calls + tool execution) before seeing the result. For complex tasks, this could take ten seconds or even longer.
\\\\n\\\\nStreaming output solves this problem: each token generated is immediately returned, allowing users to see progress in real time.
\\\\n\\\\n| Method | \\\\nUser Experience | \\\\nUse Case | \\\\n
|---|---|---|
| invoke() | \\\\nWait β See complete result at once | \\\\nScripts, API, Batch processing | \\\\n
| stream() | \\\\nSee every token in real-time | \\\\nChat interfaces, Real-time display | \\\\n
\\\\n\\\\n
stream_mode="messages" β Token-by-Token Streaming
\\\\n\\\\nThis is the finest-grained streaming mode, where each chunk corresponds to a token:
\\\\n\\\\nExample
\\\\n\\\\nfrom dotenv import load_dotenv\\\\n\\\\nload_dotenv()\\\\n\\\\nfrom langchain.agents import create_agent\\\\n\\\\nfrom langchain.chat_models import init_chat_model\\\\n\\\\nfrom langchain.messages import HumanMessage\\\\n\\\\nmodel = init_chat_model("deepseek:deepseek-v4-flash")\\\\n\\\\nagent = create_agent(\\\\n\\\\n model=model,\\\\n \\\\n system_prompt="YouisTutorial TUTORIAL ofAssistantγ",\\\\n\\\\n)\\\\n\\\\n# stream_mode="messages" return Token by Token\\\\n\\\\nprint("real-time streaming output:")\\\\n\\\\nfor msg_chunk, metadata in agent.stream(\\\\n\\\\n {"messages": [HumanMessage(content="Introduce the TUTORIAL platform in one sentence")]},\\\\n \\\\n stream_mode="messages",\\\\n\\\\n):\\\\n\\\\n # msg_chunk is AIMessageChunk\\\\n \\\\n # each chunk contains only a small piece of Content\\\\n\\\\n if msg_chunk.content:\\\\n \\\\n print(msg_chunk.content, end="", flush=True)\\\\n\\\\nprint()# final newline\\\\n\\\\nRunning result:
\\\\n\\\\nReal-time streaming output:TutorialοΌTUTORIALοΌisaforprogramming beginnersoffreeonlinelearning platformοΌprovidesRich of technical Tutorial and practical examples.\\\\n\\\\nUnderstanding metadata
\\\\n\\\\nmetadata contains the source information for this chunk:
\\\\n\\\\nExample
\\\\n\\\\nprint("view metadata information:\\\\\\\\n")\\\\n\\\\nfor msg_chunk, metadata in agent.stream(\\\\n\\\\n {"messages": [HumanMessage(content="Hello, introduce yourself")]},\\\\n \\\\n stream_mode="messages",\\\\n\\\\n):\\\\n\\\\n if msg_chunk.content and len(msg_chunk.content)>5:\\\\n \\\\n print(f"Content: {msg_chunk.content}")\\\\n \\\\n print(f"source node: {metadata.get('langgraph_node')}")\\\\n \\\\n print(f"Message Types: {type(msg_chunk).__name__}")\\\\n \\\\n break# only look at the first meaningful chunk\\\\n\\\\nRunning result:
\\\\n\\\\nView metadata information:Content: YouOkay! IisRunoobSource node: modelMessage type: AIMessageChunk\\\\n\\\\n\\\\n\\\\n
stream_mode="updates" β Step-by-Step View of Agent Execution
\\\\n\\\\nThis mode is very useful when building interfaces that need to display the "thinking process":
\\\\n\\\\nExample
\\\\n\\\\nfrom langchain.tools import tool\\\\n\\\\n@tool\\\\n\\\\ndef search_course(keyword: str) ->str:\\\\n\\\\n """search for courses in Tutorial TUTORIAL"""\\\\n\\\\n courses ={\\\\n\\\\n "python": "Python3 Basic Tutorial (30 Chapter, 20 hours)",\\\\n\\\\n "html": "HTML Basic Tutorial (25 Chapter, 15 hours)",\\\\n\\\\n }\\\\n\\\\n return courses.get(keyword.lower(),"No relevant courses found")\\\\n\\\\nagent = create_agent(\\\\n\\\\n model=init_chat_model("deepseek:deepseek-v4-flash", temperature=0),\\\\n \\\\n tools=,\\\\n \\\\n system_prompt="YouisTutorial TUTORIAL ofCourse Consultantγ",\\\\n\\\\n)\\\\n\\\\n# use updates mode to view each step\\\\n\\\\nprint("=== Agent Execute\\\\n```
YouTip