DEV Community

Cover image for From Chatbot to Agent: Building Your First Reasoning Loop in Python
Programming Central
Programming Central

Posted on Edited on

From Chatbot to Agent: Building Your First Reasoning Loop in Python

Stop writing static scripts. Start building cognitive engines using Google's Free Tier.

If you have been working with LLMs (Large Language Models), you are likely familiar with the standard request-response pattern. You send a prompt, and the model sends an answer. This is powerful, but it is passive.

In the world of AI engineering, this linear interaction is often called a Chain. It’s like an old-school GPS navigation system: it calculates a route once. If a road closes while you are driving, the old GPS doesn't know. It drives you off a cliff because it cannot observe the new reality.

This article is based on concepts from the ebook “AI Autonomous Agents with Python Programming — Volume 6”, specifically Chapter 1 and Chapter 4: here

The paradigm shift we are witnessing right now is the move from Chains to Autonomous Agents.

An Agent is like a self-driving car. It has a goal ("Go to destination"), but it constantly observes its environment, re-evaluates its plan, and acts accordingly.

In this article, we will break down the theory behind the Agentic Loop and build a pure Python agent using LangChain and Google Gemini (Free Tier).


The Theory: The Loop of Autonomy

The fundamental difference between a script and an agent is control flow.

  • Script: Input -> Step A -> Step B -> Output
  • Agent: Input -> Observe -> Think -> Act -> Loop Back

This structure relies on a continuous feedback cycle. The Agent performs an action, gets the result, and feeds that result back into its own memory to decide what to do next.

The Agentic Flow: A continuous cycle where the resulting new state feeds back into the Observation phase, closing the loop
The Agentic Flow: A continuous cycle where the resulting new state feeds back into the Observation phase, closing the loop.


Step 1: Get Your Free API Key

We will use Google Gemini 1.5 Flash because it is fast, capable, and offers a generous free tier for developers.

  1. Go to Google AI Studio.
  2. Sign in with your Google account.
  3. Click on "Get API key" (top left).
  4. Click "Create API key".

Copy this string.

Set it in your terminal:

# Mac/Linux
export GOOGLE_API_KEY="AIzaSy..."

# Windows (Powershell)
$env:GOOGLE_API_KEY="AIzaSy..."
Enter fullscreen mode Exit fullscreen mode

Step 2: The Setup

We will use the langchain-openai library. Why? Because Google provides an OpenAI-compatible endpoint, allowing us to use standard tools with Gemini models.

Install the libraries:

pip install langchain-openai langchain-core
Enter fullscreen mode Exit fullscreen mode

Example 1: The "Naive" Approach (The Failure)

Let’s look at why we need agents. Suppose we have a multi-step research goal: "Find the capital of Brazil and the year it was founded, then combine them into a sentence."

If we ask a standard LLM this in one shot without tools or a loop, it relies purely on internal training data. It cannot verify its own work.

Here is the setup using Gemini via LangChain:

import os
from langchain_openai import ChatOpenAI

# CONFIGURATION
# We use ChatOpenAI but point it to Google's infrastructure.
# Note: We append '/openai/' to the base URL for compatibility.
llm = ChatOpenAI(
    model="gemini-flash-latest",
    temperature=0.0,
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
    api_key=os.getenv("GOOGLE_API_KEY")
)

prompt = "Find the capital of Brazil and the year it was officially founded. Combine them into a sentence."

# Naive Execution
response = llm.invoke(prompt)
print(response.content)
Enter fullscreen mode Exit fullscreen mode

The problem? There is no thinking process. The script ends immediately after one guess.


Example 2: The Autonomous Agent (The Solution)

Now, let’s build a system that reasons. We will force Gemini to output a specific JSON structure: a next_action and a goal_achieved boolean.

We will put this inside a Python while loop. The script will keep running, feeding the LLM's own decisions back to it, until the agent decides it has finished the job.

Create a file named gemini_agent.py:

import os
import json
import time
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage

# --- 1. CONFIGURATION ---
llm = ChatOpenAI(
    model="gemini-flash-latest",
    temperature=0.0,
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
    api_key=os.getenv("GOOGLE_API_KEY")
)

# --- 2. THE AGENT DEFINITION ---

# The Goal
GOAL = "Research the capital city of Brazil and the year it was founded. Finally, output a summary sentence."

# The System Prompt (The Brain)
# We instruct the LLM to behave like a loop, not a chatbot.
SYSTEM_PROMPT = """
You are an autonomous research agent. You loop continuously until the goal is met.
You must output a JSON object with two keys:
1. "next_step": A string describing your next logical move (e.g., "Research capital", "Synthesize answer").
2. "is_finished": A boolean. Set to true ONLY when you have the final answer.

If "is_finished" is true, "next_step" must contain the final summary.
"""

def run_agent():
    history = [] # The agent's short-term memory
    finished = False
    iteration = 0

    print(f"--- AGENT STARTED: {GOAL} ---")

    while not finished and iteration < 5:
        iteration += 1
        print(f"\n[Loop {iteration}] Thinking...")

        # Prepare context for LangChain
        messages = [
            SystemMessage(content=SYSTEM_PROMPT),
            HumanMessage(content=f"GOAL: {GOAL}\nHISTORY: {json.dumps(history)}")
        ]

        try:
            # Call Gemini
            # We use response_format={"type": "json_object"} to ensure valid JSON
            response = llm.invoke(
                messages,
                response_format={"type": "json_object"}
            )

            # Parse logic
            content = json.loads(response.content)
            next_step = content["next_step"]
            finished = content["is_finished"]

            print(f"Decided to: {next_step}")

            if finished:
                print(f"\n*** MISSION COMPLETE ***\nFinal Output: {next_step}")
            else:
                # SIMULATING A TOOL USE
                # In a real app, you would call a Google Search API here.
                # Here, we simulate the 'tool' returning a fact to the agent.

                tool_result = ""
                step_lower = next_step.lower()

                if "capital" in step_lower:
                    tool_result = "Fact found: The capital is Brasília."
                elif "founded" in step_lower or "year" in step_lower:
                    tool_result = "Fact found: Brasília was founded in 1960."

                print(f"Tool Output: {tool_result}")

                # Add to memory so the agent knows this in the next loop
                history.append(f"Step: {next_step}. Result: {tool_result}")
                time.sleep(1)

        except Exception as e:
            print(f"Error parsing logic: {e}")
            break

if __name__ == "__main__":
    run_agent()
Enter fullscreen mode Exit fullscreen mode

How to Run It

Execute the script in your terminal:

python gemini_agent.py
Enter fullscreen mode Exit fullscreen mode

What You Will See (The Logic in Action)

You won't get an instant answer. You will see the agent thinking in steps:

  1. Loop 1: The agent decides: "I need to find the capital first."
  2. Tool Simulation: Our script catches that intent and feeds it: "The capital is Brasília."
  3. Loop 2: The agent sees its history (it knows the capital now). It decides: "Now I need the founding year."
  4. Tool Simulation: We feed it: "1960."
  5. Loop 3: The agent sees it has both facts. It decides: "I have everything. I will write the summary." It sets is_finished to True.

Why This Matters

This simple while loop is the seed of all modern AI Agents. By combining LangChain's interface with Google Gemini's speed and free tier, you can build complex, multi-step cognitive engines without worrying about token costs during development.


Take a look at my eBooks

  • Python
  • JavaScript & TypeScript
  • C# / .NET
  • Swift & Apple Platform
  • Kotlin & Android
  • Rust

Python

The Foundations of Python

Data Structures and the Standard Library

Web Development with Python
Building backend services and dynamic websites with a framework like Flask

Advanced Python & AI Integration
Deep dive into OOP, decorators, asyncio, and orchestrating LLMs with LangChain.

Gemini 3 Python Programming - The Complete Guide
Agents, Veo 3.1, Lyria, Nano Banana/Pro, Function Calling, Grounding, Computer Use and Robotics

AI Autonomous Agents with Python Programming
Master LangGraph, CrewAI, and RAG to Build Self-Correcting Swarms and Autonomous Digital Workers

Finance & AI Trading with Python Programming
Master Algorithmic Trading, Financial NLP, and Vectorized Backtesting to Build Autonomous 'News + Math' Strategies

Cloud-Native Python, DevOps & LLMOps. Containerization, Kubernetes, and Serving AI Models at Scale
From Docker and Kubernetes to Serving LLMs with Pulumi

Defensive Cybersecurity with Python Programming
A Practical Guide to System Monitoring, Network Defense, and Automated Security Hardening

Data Science & Analytics with Python Programming

Neural Networks & Deep Learning with Python Programming

Architecting Neuro-Symbolic Agents with Python Programming
Integrating LLMs, Wolfram Alpha, IBM Watson and Open Source Stacks for Near-Zero Hallucination Systems

Bioinformatics & AI with Python Programming
Master Genomic Data Science, Protein Folding with AlphaFold, and AI-Driven Drug Discovery

Geospatial AI (GeoAI) with Python Programming
Building Autonomous GIS Agents, Deep Learning Models, and Interactive Dashboards

Astrophysics & AI with Python Programming
Building Research Agents for Astronomy, Cosmology, and SETI

Open-Source LLMs & Local Fine-Tuning
Mastering LoRA, vLLM, Ollama, and Custom SLMs

Unsloth: Efficient Fine-Tuning for Large Language Models
Methods and Workflows for Fine-Tuning and Deploying Large Language Models on Limited Hardware

Hermes Agent: The Self-Evolving AI Workforce
Architecting Autonomous Systems that Learn, Remember, and Grow.

Frontier AI Safety, Mechanistic Interpretability & Alignment Engineering
Inspecting Neural Circuits, Steering Vectors, Autonomous Capability Evals, and Scalable Oversight for Superintelligent Systems.


JavaScript & TypeScript

Foundations
OpenAI API, Zod, and LangChain.js

The Modern Stack
Building Generative UI with Next.js, Vercel AI SDK, and React Server Components.

Master Your Data
Production RAG, Vector Databases, and Enterprise Search.

Autonomous Agents
Building Multi-Agent Systems and Workflows with LangGraph.js

The Edge of AI
Local LLMs (Ollama), Transformers.js, WebGPU, and Performance Optimization

The AI-Ready SaaS Boilerplate. Auth, Database with Vector Support, and Payment Stack
Auth, Database with Vector Support, and Payment Stack.

Backend for Frontend & Intelligent APIs. tRPC, Edge Functions, and LLM Data Transformation
tRPC, Edge Functions, and LLM Data Transformation.

The Monetization Engine. Stripe, Smart Dunning, and AI Customer Support Agents
Stripe, Smart Dunning, and AI Customer Support Agents.

AI-Driven Growth Engineering. Programmatic SEO with GPT-4, Content Automation, and Analytics.
Programmatic SEO with GPT-4, Content Automation, and Analytics.

No More Localhost. Mastering Docker, Linux, and Containerization for JS & AI Apps
Mastering Docker, Linux, and Containerization for JS & AI Apps.

The Perfect Pipeline. Advanced CI/CD with GitHub Actions, Automated Testing, and AI Code Reviews
Advanced CI/CD with GitHub Actions, Automated Testing, and AI Code Reviews.

Kubernetes & Orchestration. Deploying Scalable Node.js & AI Clusters without Tears
Deploying Scalable Node.js & AI Clusters without Tears.

React Native for Web Developers
From Next.js to Expo, NativeWind, and Universal App

Offline AI & Local LLMs. Running Llama 3 and Vector Search directly on the Smartphone
Running Llama 3 and Vector Search directly on the Smartphone.

App Store Engineering. CI/CD for Mobile (EAS), OTA Updates, and AI-Driven App Store Optimization
CI/CD for Mobile (EAS), OTA Updates, and AI-Driven App Store Optimization.

The TypeScript-First Architect. Building Robust Applications with Effect, Zod, and Drizzle
Building Robust Applications with Effect, Zod, and Drizzle.

The Native Era. Modern Node.js, Bun & Deno without Bundlers or Transpilers
Modern Node.js, Bun & Deno without Bundlers or Transpilers.

Local-First Systems in TypeScript. Collaborative & Offline-Ready Web Apps with CRDTs and WASM DBs
Collaborative & Offline-Ready Web Apps with CRDTs and WASM DBs.

TypeScript Metaprogramming. Advanced Type Gymnastics, Modern Decorators, and Compiler Internals
Advanced Type Gymnastics, Modern Decorators, and Compiler Internals.

Model Context Protocol (MCP) & Computer Use. Standardizing Tool Integration, Vision-Driven Browser Automation, and Agent Governance in TypeScript

Standardizing Tool Integration, Vision-Driven Browser Automation, and Agent Governance in TypeScript.

Generative Media & Visual Workflow Engines. Node-Based AI Canvases, Real-Time Media Streaming Pipelines, and WebGPU Processing in TypeScript
Node-Based AI Canvases, Real-Time Media Streaming Pipelines, and WebGPU Processing in TypeScript.

Neuro-Symbolic AI & Knowledge Graphs. Deterministic Solvers, GraphDBs, Ontologies, and Zero-Hallucination Architectures
Deterministic Solvers, GraphDBs, Ontologies, and Zero-Hallucination Architectures in TypeScript.

Event-Driven Architecture & DDD in TypeScript. Event Sourcing, CQRS, and Microservices at Scale
Event Sourcing, CQRS, and Microservices at Scale.

Building Desktop Apps & Developer Tools with Tauri 2.0, Rust, and TypeScript
Cross-platform desktop tools with Tauri, Rust, and TypeScript.

FinTech Architecture in TypeScript. Precision Math, Double-Entry Ledgers, and High-Reliability Payment Pipelines
Precision Math, Double-Entry Ledgers, and High-Reliability Payment Pipelines.

Hardened TypeScript. Passkeys, Supply Chain Defense, and Zero-Trust Architectures
Passkeys, Supply Chain Defense, and Zero-Trust Architectures.

Spatial Web Development. Building Interactive 3D and WebXR Experiences with React Three Fiber & TypeScript
Building Interactive 3D and WebXR Experiences with React Three Fiber & TypeScript.

Multiple-choice test book for: Foundations (Volume 1)

Spatial Web Development. Building Interactive 3D and WebXR Experiences with React Three Fiber & TypeScript
Building Interactive 3D and WebXR Experiences with React Three Fiber & TypeScript.

Jev: The Definitive Guide to System One AI
Building Sub-100ms Decision Engines, Calibrated Guardrails, and Two-Speed Architectures with Jev and Generative LLMs


C# / .NET

Get all the Ten C# & AI volumes at a discounted price, or choose an ebook:

The Foundations
Syntax, Type System, and Logic for Modern Developers.

Advanced OOP & AI Data Structures
Modeling Complex Systems and Tensors.

Data Manipulation, LINQ & Vectors
From Collections to AI Embeddings

Asynchronous AI Pipelines
Async/Await, Parallelism, and Streaming LLM Responses.

Building AI Web APIs with ASP
NET Core. Serving Models and Chat Endpoints

Intelligent Data Access with EF Core
Vector Databases, RAG, and Memory Storage.

Cloud-Native AI & Microservices
Containerizing Agents and Scaling Inference.

The Core of AI Engineering: Microsoft Semantic Kernel & Agentic Patterns

Edge AI & Local Inference
Running LLMs (Llama/Phi) locally with C# and ONNX.

High-Performance C# for AI
Span, SIMD, and Optimizing Token Processing

Full Stack AI with Blazor. Building Interactive Copilots and WASM AI
Building Interactive Copilots and WASM AI.

Enterprise AI Integration & Process Automation. Connecting LLMs to legacy systems, internal APIs, and real-world business processes
Connecting LLMs to legacy systems, internal APIs, and real-world business processes.

AI for Game Development & Interactive Simulation. Using LLMs and generative AI to create dynamic worlds and intelligent characters in Unity
Using LLMs and generative AI to create dynamic worlds and intelligent characters in Unity.


Swift & Apple Platform

Get the Swift & AI 10 Volumes Bundle at a discoutend price, or choose an ebook:

Core ML & Vision Framework
On-device image classification, object detection, and custom model integration with Core ML and Vision.

Apple Intelligence & Foundation Models
Building apps with Apple's on-device LLM APIs, Writing Tools, and the Apple Intelligence framework

Natural Language & Speech
NLP, sentiment analysis, text classification, and Speech-to-Text with Apple's Natural Language and Speech frameworks.

SwiftUI for AI Apps
Building reactive, intelligent interfaces that respond to model outputs, stream tokens, and visualize AI predictions in real time

Create ML Studio
Training custom models without Python: tabular, image, sound, and motion classifiers using Create ML in Swift.

MLX Swift & Local LLMs. Deep dive into Apple's MLX framework for high-performance machine learning.
Building custom inference engines, fine-tuning local models (LoRA), and leveraging Unified Memory directly from Swift.

visionOS & Spatial AI with Swift

Swift + OpenAI & LangChain
Integrating external LLM APIs, RAG pipelines, and agentic workflows in iOS and macOS apps

CoreData, CloudKit & Vector Search

Shipping AI Apps to the App Store


Kotlin & Android

On-Device GenAI with Android Kotlin
Mastering Gemini Nano, AICore, and local LLM deployment using MediaPipe and Custom TFLite models

Edge AI Performance with Android Kotlin
Optimizing hardware acceleration via NPU, GPU, and DSP. Advanced quantization and model pruning

Android AI Agents
Building autonomous apps that use Tool Calling, Function Injection, and Screen Awareness to perform tasks for the user


Rust

Rust Advanced Memory Patterns for AI
Mastering Lifetimes, Smart Pointers, and custom allocators for managing large models and datasets

Extending Python with Rust. Creating high-performance Python modules with PyO3.
Creating high-performance Python modules with PyO3 for data processing, tokenization, and inference, replacing slow Python code.


Top comments (0)