Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome back to our exploration of agent capabilities.
In our last lesson, you successfully implemented the fundamental function calling loop. You learned how to define tools, have an LLM decide to use one, execute it in your application, and feed the result back to the model for a final, synthesized answer. This covered single actions and parallel actions.
Today, we're taking the next logical step. What happens when a query is too complex to be answered by a single tool call? For instance, a query like "What's the current weather in the city where the Eiffel Tower is located?" requires a sequence of actions: first, identify the city (Paris), and then get the weather for that city.
Our learning outcome is to handle multi-step tool chains where one tool's output feeds into another. This is the essence of an agent's ability to reason and form a plan, moving beyond simple reflexes to tackle more sophisticated problems.
The Agentic Loop: From Single Action to a Chain
The single tool call you implemented last time is one iteration of a fundamental pattern in AI agents called the agentic loop. The core idea is a cycle of reasoning and interacting with an environment.

In our last lesson, this loop ran exactly once:
- Plan: The LLM decided to call a tool.
- Act: Your code executed the tool.
- Observe: Your code fed the result back to the LLM.
- Final Answer: The LLM, having all the info, stopped the loop and gave an answer.
A multi-step chain is simply this same loop running multiple times. After observing the result of the first tool, the LLM might decide its next plan is to call another tool, using the information it just received. This continues until it has enough information to provide a final answer.
Managing the state (the history of messages, tool calls, and tool results) through these cycles is critical. While you can build this logic from scratch, specialized frameworks exist to make it much more manageable. We'll be looking at LangGraph, an extension of the LangChain library, which is purpose-built for creating these stateful, graph-based agent workflows.
A Concrete Example: The Two-Step Weather Query
Let's build a practical example to make this concrete. We want an agent that can answer: "What's the current weather like in Karachi?"
To a human, this is one step. But for a computer with specific tools, it's a two-step process:
- Tool 1: Geocoding. Convert the string "Karachi" into geographic coordinates (latitude and longitude).
- Tool 2: Weather API. Use the coordinates from Step 1 to call a weather service and get the current conditions.
The output of the first tool is the direct input for the second. Let's see how this is implemented.
The following article provides a superb, code-centric walkthrough. As a software engineer, you'll appreciate its Pydantic-based, structured approach. First, let's examine how the tools are defined to facilitate this chain.
Building LLM agents to validate LangGraph tool use and structured ...
The article 'Building LLM agents to validate LangGraph tool use and structured ...' from the CircleCI blog provides a fantastic, practical implementation. We'll start by examining the tools themselves. Notice how one tool's output (Coordinates) is designed to be the input schema for the other.
Read the section 'Building structured tools with Pydantic'. Pay close attention to the Python code defining get_coordinates_from_city and get_current_weather. See how the Coordinates Pydantic model, which is the output of the first tool, matches the input arguments latitude and longitude for the second.
Now that we have the tools, we need an orchestrator to manage the flow between them. This is where LangGraph comes in. It builds a state machine where nodes represent actions (like calling the LLM or executing a tool) and edges represent the transitions between them.
This next section of the article explains how to build the agent and its workflow.
Building LLM agents to validate LangGraph tool use and structured ...
Let's see how LangGraph wires these tools into a reactive loop. This section explains how the agent's state is managed and how the graph is constructed to allow for sequential reasoning and tool calls.
Read the section 'Building your agent with LangGraph'. The most crucial part to understand is the _build_workflow method. Specifically, the line workflow.add_edge('tools', 'agent') is the key to creating a chain. It creates a cycle, forcing the workflow back to the 'agent' (the LLM) after a tool has been executed, allowing the agent to process the result and decide on the next step.
To summarize the flow for the query "What's the weather in Karachi?":
- The user's query enters the
"agent"node. The LLM sees it needs coordinates and decides to callget_coordinates_from_city. - The graph's logic routes to the
"tools"node, which executes the function. The output is a JSON object with Karachi's latitude and longitude. - The edge from
"tools"back to"agent"sends this coordinate data back into the workflow. - The
"agent"node is invoked again. The LLM now has the original query and the coordinates. It sees the next logical step is to get the weather. It callsget_current_weatherwith the coordinates it just received. - The process repeats: the
"tools"node executes the weather tool and returns the current weather data. - This result is sent back to the
"agent"node. The LLM now has all the necessary information. Instead of calling another tool, it generates the final, human-readable answer.
The best way to be sure this is happening is to test it. The article provides an excellent end-to-end test that traces the agent's execution path, proving the chain works as intended.
Building LLM agents to validate LangGraph tool use and structured ...
This final reading from the article demonstrates how to test the multi-step behavior. It validates that the agent performs the two steps in the correct order, passing the output of the first as input to the second.
Read through the test test_weather_query_success_workflow in the 'End-to-end workflow testing with weather tool' section. Observe how the test asserts the call to get_coordinates_from_city, then validates its output, and finally asserts the call to get_current_weather using those exact coordinates. This confirms the chaining mechanism.
Advanced Chains: Dynamic and Iterative Workflows
The simple A -> B chain is just the beginning. The agentic loop, especially when managed by a framework like LangGraph, can create far more complex and dynamic workflows.
The video below provides a great conceptual overview of the fundamental agent loop and then explores more advanced patterns.
Building Effective Agents with LangGraph
The video 'Building Effective Agents with LangGraph' provides an excellent conceptual overview of different workflow patterns. First, let's watch the section that defines the fundamental agent loop, which is the engine driving these chains.
Watch from 25:03 to 29:02. The key idea is the cycle: the LLM decides on a tool, a ToolNode executes it, and the 'environmental feedback' (the result) is returned to the LLM to decide the next step. The example shows adding and then multiplying, another simple A -> B chain.
This same looping mechanism enables more powerful behaviors:
- Orchestrator/Worker: An LLM planner acts as a project manager. It receives a complex task (e.g., "write a report on scaling laws"), breaks it down into sub-tasks (introduction, fundamentals, etc.), and dynamically spawns "worker" agents to handle each part.
- Evaluator/Optimizer: An agent enters a loop where one part generates a response (e.g., writes a piece of code or a joke), and another part evaluates it against certain criteria. If it fails the evaluation, it's sent back for revision. The loop continues until the output is satisfactory.
Let's watch a brief overview of these patterns. You don't need to absorb all the code; focus on understanding the workflow possibilities.
Building Effective Agents with LangGraph
Now let's look at these more dynamic patterns. This segment shows an 'Orchestrator/Worker' model, where the LLM itself dynamically creates the steps, and an 'Evaluator' loop, where the agent iterates until a quality condition is met.
Watch from 13:45 to 23:04. Focus on the concepts: Orchestrator/Worker: Notice how the planner LLM first generates a list of report sections, and then worker nodes process each one. The chain's structure is defined at runtime by the LLM. Evaluator: See how the graph can loop back on itself based on a 'grade' from an evaluator model, creating an iterative refinement process.
These advanced patterns demonstrate that by chaining tools within an agentic loop, you can move from simple command execution to sophisticated, goal-oriented problem-solving.
Conclusion
In this lesson, we elevated our agent's capabilities from single actions to multi-step reasoning. You learned how to create tool chains where the output of one step becomes the critical input for the next.
Key Takeaways:
- Multi-step tasks are handled by running the agentic loop (Plan -> Act -> Observe) multiple times.
- The agent's state, which includes the full history of actions and their results, is maintained across each step of the chain.
- Frameworks like LangGraph are designed to manage this stateful, cyclical logic by defining workflows as graphs of nodes and edges.
- The key to chaining is the loop: after a tool executes, its output is returned to the LLM, which then decides the next action based on this new information.
- This pattern enables not only simple sequential chains (
A -> B) but also dynamic and iterative workflows like orchestrator/worker and evaluator loops.
You can now build agents that can decompose a problem, execute a plan, and use intermediate results to inform subsequent actions.
In our next lesson, we'll focus on making these chains more robust. We will cover error handling and validation for tool call results, addressing what to do when tools fail, return unexpected data, or encounter other real-world issues.