stream_mode: each step as it happens
invoke hands back the finished conversation. stream hands back each step as it happens, which is how you see what an agent is doing and why it stopped.
Every agent from lesson 7 on can be watched this way, so it is worth learning before anything goes wrong. The agent is lesson 7's, without the system prompt.
from langchain.agents import create_agent
from shop_model import ShopModel
from tools import lookup_order
agent = create_agent(ShopModel(), tools=[lookup_order])One tool and one model, and nothing wrapped around the call. Enough to see every step the loop takes.
One update per step
question = {"messages": [{"role": "user", "content": "Where is my order A17?"}]}
for step in agent.stream(question, stream_mode="updates"):
for name, update in step.items():
for message in update["messages"]:
print(f"{name:<6} {message.type:<4} {message.text or message.tool_calls}")With stream_mode="updates", each item is one step: the name of the part of the agent that ran, and the messages it added. model is a model call and tools is the tools running. Three steps for one question: ask, look up, answer.
Reading the finished conversation
result = agent.invoke({"messages": [{"role": "user", "content": "Where are A17 and C40?"}]})
for message in result["messages"]:
message.pretty_print()Two orders in one question gave two tool calls in one AI message, and two tool messages back. pretty_print from lesson 2 shows the tool calls with their arguments and ids, which is often enough to see why an agent did what it did.
- Stream a question with no order in it and count the steps.
- Stream with
stream_mode="values"and print how many messages each item holds. - Stream a question about B22 and find the step where the tool says it has no such order.
Slow is fine. Stopping is the only problem.