Reason → Act → Observe: Building an Agentic Loop with LangChain and Python

This article explains what an agentic loop is, contrasts it with single‑pass chatbots, outlines its five stages, shows a visual architecture, walks through a concrete multi‑step example, provides Python pseudocode and a LangChain implementation, and discusses when to use or avoid such loops.

DeepHub IMBA
DeepHub IMBA
DeepHub IMBA
Reason → Act → Observe: Building an Agentic Loop with LangChain and Python

Simple Chatbot vs. AI Agent

Standard chatbots operate in a single‑pass manner: the user sends a message, the LLM generates a response, and the interaction ends, with no state carried forward. An AI agent, by contrast, is designed to act and therefore follows a while loop.

Agentic Loop Architecture

The agentic loop is an iterative process where the LLM uses tools, adjusts based on feedback, and repeats until the task is fully completed.

The Five Stages of an Agentic Loop

Perceive : The agent receives input such as a user prompt, an API response, or an error message.

Reason : The LLM processes the context and decides the next step.

Plan : For complex goals, the agent decomposes the goal into smaller sub‑tasks.

Act : The agent executes concrete actions—running code, querying a database, or calling an API.

Observe : The agent examines the result, determines if it succeeded, and decides whether the plan needs adjustment.

In most tasks the core loop can be simplified to three continuous steps: Reason → Act → Observe.

Architecture / Diagram

Below is a simple visual illustration of the architecture:

And a more detailed diagram of the agentic loop:

How the Loop Runs: A Simple Example

Task: “Find the most‑cited 2026 paper on agent memory and summarize it.”

Iteration 1 (Reason → Act → Observe): The agent reasons it must search 2026 papers, calls a search API, and observes a list of 15 papers.

Iteration 2: The agent reasons it needs the full text of the most‑cited paper, calls a document retrieval tool, and observes the abstract.

Iteration 3: The agent decides the information is sufficient, generates the summary, and ends the loop.

Underlying Python pseudocode:

while not done:
    response = call_llm(messages)
    if response has tool_calls:
        results = execute_tools(response.tool_calls)
        messages.append(results)
    else:
        done = True
        return response

Implementing a Basic Version with LangChain

The following Python code uses LangChain and an Oracle database connection as a tool for the agentic loop.

from langchain.agents import create_agent
from langchain_core.tools import tool
from langchain_core.messages import AIMessage, ToolMessage

# 1. Define tools the agent can ACT
@tool
def calculate(expression: str) -> str:
    """Evaluate a mathematical expression."""
    pass  # implementation omitted

@tool
def timezone_convert(time_str: str, from_city: str, to_city: str) -> str:
    """Convert local time between cities."""
    pass  # implementation omitted

# 2. Create an agent with tools (this builds a StateGraph loop)
agent = create_agent(
    model=llm,
    tools=[calculate, timezone_convert],
    system_prompt="You are a precise assistant. Use tools to find answers."
)

# 3. Run the iterative loop
QUESTION = "If I fly from London at 14:00 for 6 hours, what time do I land in New York?"

for chunk in agent.stream({"messages": [("human", QUESTION)]}, stream_mode="values"):
    last_msg = chunk["messages"][-1]
    if isinstance(last_msg, AIMessage) and last_msg.tool_calls:
        for call in last_msg.tool_calls:
            print(f"[ACT] → Executing {call['name']}")
    elif isinstance(last_msg, ToolMessage):
        print(f"[OBSERVE] ← Result received")
    elif isinstance(last_msg, AIMessage) and last_msg.content:
        print(f"
Final Answer: {last_msg.content}")

When to Use and When Not to Use an Agentic Loop

Suitable scenarios:

When the number of steps required to complete a task cannot be predetermined.

When the system must adjust its strategy based on intermediate results (e.g., retrying with different keywords after a failed search).

When task completeness outweighs raw speed.

Unsuitable scenarios:

Fixed‑sequence workflows where the process is highly predictable; a deterministic pipeline is preferable.

Simple tasks solvable with a single LLM call and one tool call; the loop adds unnecessary overhead.

Strict latency constraints; each iteration incurs an LLM call, increasing response time and token cost.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAI agentsLLMtool integrationLangChainAgentic Loop
DeepHub IMBA
Written by

DeepHub IMBA

A must‑follow public account sharing practical AI insights. Follow now. internet + machine learning + big data + architecture = IMBA

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.