0
0
LangChainframework~10 mins

Structured chat agent in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Structured chat agent
User Input
Parse Input
Select Tool/Chain
Execute Tool/Chain
Collect Output
Format Response
Return to User
The structured chat agent takes user input, decides which tool or chain to run, executes it, and returns the formatted answer.
Execution Sample
LangChain
from langchain.agents import StructuredChatAgent
from langchain.chat_models import ChatOpenAI

agent = StructuredChatAgent.from_llm_and_tools(ChatOpenAI(temperature=0), tools)
response = agent.invoke({'input': 'What is the weather today?'})
This code creates a structured chat agent with a chat model and tools, then sends a user question to get a response.
Execution Table
StepActionInput/StateOutput/State
1Receive user input{'input': 'What is the weather today?'}User query stored
2Parse inputUser query storedParsed intent and entities
3Select toolParsed intent and entitiesWeather tool selected
4Execute toolWeather tool, queryWeather data fetched
5Format responseWeather dataFormatted answer string
6Return responseFormatted answer stringResponse sent to user
💡 Response returned to user, ending this interaction cycle
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
user_inputNone'What is the weather today?''What is the weather today?''What is the weather today?''What is the weather today?''What is the weather today?'
parsed_intentNone'weather_query''weather_query''weather_query''weather_query''weather_query'
selected_toolNoneNone'weather_tool''weather_tool''weather_tool''weather_tool'
tool_outputNoneNoneNone'Sunny, 25°C''Sunny, 25°C''Sunny, 25°C'
formatted_responseNoneNoneNoneNone'The weather today is sunny with 25°C.''The weather today is sunny with 25°C.'
Key Moments - 2 Insights
How does the agent know which tool to use for the user's question?
The agent parses the input to find the intent and entities (see Step 2 and Step 3 in the execution table), then selects the appropriate tool based on that intent.
What happens if the tool execution fails or returns no data?
The agent would handle this by formatting a fallback response or error message during the formatting step (Step 5), ensuring the user still gets a reply.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after Step 4?
A'Sunny, 25°C'
B'weather_tool'
C'Parsed intent and entities'
D'Formatted answer string'
💡 Hint
Check the 'Output/State' column for Step 4 in the execution table.
At which step does the agent decide which tool to use?
AStep 5
BStep 2
CStep 3
DStep 6
💡 Hint
Look for the step labeled 'Select tool' in the execution table.
If the user input changes, which variable in the variable tracker changes first?
Aparsed_intent
Buser_input
Cselected_tool
Dtool_output
💡 Hint
Check the variable tracker for the variable that stores the raw user question.
Concept Snapshot
Structured chat agent flow:
1. Receive user input
2. Parse input for intent
3. Select appropriate tool
4. Run tool to get data
5. Format response
6. Return answer
Use langchain's StructuredChatAgent with chat models and tools for modular, clear conversations.
Full Transcript
A structured chat agent in langchain works by taking the user's question, understanding what the user wants, choosing the right tool to answer, running that tool, and then giving back a clear answer. The process starts with receiving input, then parsing it to find the intent. Next, the agent picks the tool that fits the intent, runs it to get information, formats the answer nicely, and finally sends it back to the user. Variables like user_input, parsed_intent, selected_tool, tool_output, and formatted_response change step by step as the agent works. This flow helps keep conversations organized and easy to manage.