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.
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.
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.
- Go to Google AI Studio.
- Sign in with your Google account.
- Click on "Get API key" (top left).
- 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..."
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
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)
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()
How to Run It
Execute the script in your terminal:
python gemini_agent.py
What You Will See (The Logic in Action)
You won't get an instant answer. You will see the agent thinking in steps:
- Loop 1: The agent decides: "I need to find the capital first."
- Tool Simulation: Our script catches that intent and feeds it: "The capital is Brasília."
- Loop 2: The agent sees its history (it knows the capital now). It decides: "Now I need the founding year."
- Tool Simulation: We feed it: "1960."
- Loop 3: The agent sees it has both facts. It decides: "I have everything. I will write the summary." It sets
is_finishedto 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.
This article is based on concepts from the ebook “AI Autonomous Agents with Python Programming — Volume 6”, specifically Chapter 1 and Chapter 4: https://leanpub.com/AIAutonomousAgents or https://www.amazon.com/dp/B0G5Z4XM74, you can read it standalone.
Explore the complete 11-volume “Python Programming Series” ebooks for a comprehensive journey from Python fundamentals to advanced AI deployment, Gemini 3, LLMops, AI trading and much more: https://www.amazon.com/dp/B0FTTQNXKG . Each book can be read as a standalone.
Follow me to stay updated on upcoming articles.

Top comments (0)