#atom

Methods for incrementally delivering agent execution results for better user experience

Core Idea: Agent streaming modes provide different ways to deliver intermediate results during agent execution, allowing for real-time visibility into the agent's reasoning process, tool usage, and gradual response generation.

Key Elements

Implementation Example

from langchain_anthropic import ChatAnthropic
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent

# Create agent components
model = ChatAnthropic(model_name="claude-3-sonnet-20240229")
search = TavilySearchResults(max_results=2)
tools = [search]
agent_executor = create_react_agent(model, tools)

# Stream complete messages at each step (values mode)
for step in agent_executor.stream(
    {"messages": [HumanMessage(content="What's the weather in SF?")]},
    stream_mode="values"
):
    # Process each complete message
    step["messages"][-1].pretty_print()

# Stream individual tokens as they're generated (messages mode)
for step, metadata in agent_executor.stream(
    {"messages": [HumanMessage(content="What's the weather in SF?")]},
    stream_mode="messages"
):
    if metadata["langgraph_node"] == "agent" and (text := step.text()):
        print(text, end="")  # Print tokens as they arrive

Applications

Connections

References

  1. LangGraph documentation on streaming modes
  2. LangChain Python documentation on agent execution

#streaming #agents #langchain #langgraph #user-experience #real-time-ai


Connections:


Sources: