Step-by-Step Tutorial: Building an Agentic AI with Wikipedia Search Tools and LangChain Memory in Python



Key Takeaways

  • Standard LLMs are stateless, meaning they have no memory or access to real-time information, limiting their usefulness for dynamic tasks.
  • An agentic AI overcomes this by giving an LLM access to tools (like a Wikipedia search) and memory (to recall conversation history).
  • This tutorial shows you how to build a research assistant in Python using LangChain that can autonomously find current information and understand follow-up questions.

I once asked a standard chatbot for the current price of Bitcoin. It apologized profusely, explaining its knowledge was cut off in early 2023 and it couldn't access real-time information. It was like talking to a brilliant historian who had been locked in a library for two years—incredibly smart, but completely clueless about the present.

This is the fundamental limitation of most Large Language Models (LLMs). They're powerful but passive, trapped in the past and unable to do anything. But what if we could give that historian a phone, a library card to the entire internet, and a notebook to remember your conversations?

That's exactly what we're going to do today. We're moving beyond simple Q&A bots and building an agentic AI. This is an AI that doesn't just talk; it thinks, acts, and remembers.

Introduction: Why Build an AI with Memory and Tools?

The Problem: Stateless LLMs and the Knowledge Cutoff

Standard LLMs are "stateless." Each time you send a prompt, it's like they're meeting you for the first time. They have no memory of your previous questions beyond the immediate context window and no access to the outside world.

This makes them great for generating text but terrible for tasks that require up-to-date information or a sequence of actions.

The Solution: Agentic AI that Can Act and Remember

Agentic AI flips this on its head. It’s an autonomous system that can reason, plan, and execute actions to achieve a goal.

By giving an LLM access to tools (like a Wikipedia search API) and memory, we empower it to break free from its static knowledge base. It can proactively find information, perform tasks, and learn from its interactions with you. This is the leap from a simple chatbot to a genuine digital assistant.

What We'll Build: A Wikipedia Research Assistant

In this tutorial, I'll walk you through building a simple but powerful agentic AI using Python and LangChain. This agent will be able to use the Wikipedia API to find real-time answers, remember the context of your conversation, and use that memory to answer follow-up questions intelligently.

Let's get our hands dirty!

Prerequisites & Environment Setup

Before we start coding, we need to get our digital workspace in order. It's quick and painless, I promise.

What You'll Need (Python, Pip)

Make sure you have a modern version of Python installed (I'm using Python 3.9+). You'll also need pip, Python's package manager, which usually comes bundled with Python.

Installing LangChain, OpenAI, and Wikipedia Libraries

Open your terminal or command prompt and run this one command. It'll grab all the necessary libraries we need to build our agent.

pip install langchain langchain-openai wikipedia langchain-community

This installs the core LangChain framework, the OpenAI integration for the "brain," the Wikipedia library for our "tool," and some community components.

Securing Your OpenAI API Key

Our agent's reasoning power will come from an OpenAI model like GPT-4o mini. For that, you need an API key.

  1. Head over to the OpenAI API keys page and create a new secret key.
  2. For this code to work, you must set this key as an environment variable named OPENAI_API_KEY. This is far more secure than pasting it directly into your code.

Core Concepts: The Building Blocks of Our Agent

It's crucial to understand the "why" behind the code. Our agent is built on three pillars: Agents, Tools, and Memory.

Agents: The 'Brain' that Makes Decisions

The "agent" is the core logic loop. It uses the LLM not just to answer, but to decide what to do next. When you ask a question, the agent analyzes it and thinks, "Should I just answer this from my existing knowledge, or do I need to use one of my tools?"

Tools: The 'Hands' that Interact with the World

Tools are what give our agent its superpowers. A tool can be anything: a search engine, a calculator, a database connection, or an API call. In our case, our agent's primary tool is the Wikipedia search.

This is what allows it to access current, factual information beyond its training data. Giving an agent tools is one of two primary ways to ground it in reality.

The other popular approach is Retrieval-Augmented Generation (RAG), which involves feeding the AI documents from a private knowledge base. RAG is fantastic for specialized knowledge, while tools are better for dynamic, real-world actions.

Memory: Giving Our Agent a Sense of History

Memory is the final piece of the puzzle. Without it, our agent would be just as forgetful as a standard chatbot.

We'll use ConversationBufferMemory from LangChain, which simply stores the entire conversation history. When our agent decides its next action, it looks at both your new prompt and the memory of what you've already discussed.

Step-by-Step Implementation in Python

Alright, theory's over. Let's write some code.

Step 1: Initializing the LLM

First, we need to import our libraries and initialize the LLM that will serve as our agent's brain. I'm using gpt-4o-mini because it's fast, cheap, and incredibly smart.

import os
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain_community.tools import WikipediaQueryRun
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder

# Make sure you've set your OPENAI_API_KEY environment variable!
# os.environ["OPENAI_API_KEY"] = "your-key-here" 

# Initialize the LLM - this is the core reasoning engine.
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

I set temperature=0 to make the model's responses more deterministic and factual, which is what we want for a research assistant.

Step 2: Defining the Wikipedia Search Tool

Next, we define the tools our agent can use. For now, it's just one: the Wikipedia search tool. LangChain makes this ridiculously easy.

# Create the Wikipedia tool.
# This single line gives our agent the ability to search Wikipedia.
wikipedia_tool = WikipediaQueryRun()
tools = [wikipedia_tool]

That's it! The agent now knows it has a tool named wikipedia at its disposal.

Step 3: Integrating ConversationBufferMemory

Now, let's give our agent a memory. We'll initialize ConversationBufferMemory to store our chat history.

# Set up memory. The `memory_key` must match the variable in the prompt.
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

The return_messages=True part is important; it ensures the memory is stored in a format the chat model understands.

Step 4: Assembling and Initializing the Agent

This is where we bring everything together. We create a prompt template that tells the agent how to behave, what tools it has, and where to find the conversation history.

# The prompt is the agent's instruction manual.
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an agentic AI researcher. Use the Wikipedia tool to answer queries. Remember context and think step-by-step."),
    MessagesPlaceholder(variable_name="chat_history"), # Where memory goes
    ("user", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad"), # Where the agent's internal thoughts go
])

# Create the agent itself
agent = create_tool_calling_agent(llm, tools, prompt)

# Create the Agent Executor, which is the runtime for the agent.
agent_executor = AgentExecutor(
    agent=agent, 
    tools=tools, 
    memory=memory, 
    verbose=True # Set to True to see the agent's thought process
)

Then we combine the LLM, tools, and prompt to create the agent executor.

Step 5: Creating the Main Execution Loop

Our agent is built! Now we just need to interact with it. I'll use a simple invoke call to send prompts.

# This is how you run the agent.
# We'll put our questions here.

Testing Our Agent: A Sample Research Session

Let's see this thing in action. I'm going to ask it an initial question and then a follow-up that relies on the context from the first.

Asking an initial question and seeing the tool in action

Let's start with a factual query.

# First question
response1 = agent_executor.invoke({"input": "Who was Alan Turing and what were his key contributions?"})

print(response1['output'])

If you watch the verbose=True output, you'll see something amazing. The agent will identify that it needs to search Wikipedia, call the tool with the query "Alan Turing," get the result, and then synthesize an answer. It's taking autonomous action!

Asking a follow-up question that relies on memory

Now for the real test. Can it remember who we're talking about?

# Follow-up question
response2 = agent_executor.invoke({"input": "Based on that, how did his work influence modern AI?"})

print(response2['output'])

Analyzing the Agent's 'Thought Process' Output

Because we have memory integrated, the agent will look at the new question and the previous conversation about Alan Turing. It will understand that "his work" refers to Turing's work. This demonstrates both memory and tool use in a single, elegant flow.

Conclusion and Your Next Steps

Recap: What We Accomplished

Congratulations! You've just built a genuine agentic AI. It demonstrates the core principles: it can reason about a task, autonomously use a tool to gather external information, and maintain a memory of the conversation to handle context. This is a massive step up from a basic chatbot.

How to Expand Your Agent (Adding More Tools)

The beauty of this framework is its modularity. Want to give your agent more skills? Just add more tools to the tools list!

You could add: * A calculator tool for math problems. * A search tool for Google instead of Wikipedia. * A custom tool that calls a private API.

The possibilities are endless. You're only limited by what you can define as a function.

Troubleshooting Common Errors

  • API Key Error: If you get an authentication error, double-check that your OPENAI_API_KEY environment variable is set correctly.
  • Tool Not Found: Make sure the tool name is spelled correctly and the tool is included in the tools list passed to the AgentExecutor.
  • Rate Limits: If you're on a free OpenAI tier, you might hit rate limits. Just wait a minute and try again.

This simple research bot is just the beginning. You could wrap this logic in a Flask or FastAPI server and turn it into a real application. Go ahead, start building



Recommended Watch

πŸ“Ί 25 Tools πŸ”« for Agents 🀡 in LangChain 🦜 | Wikipedia Integration | Use AI
πŸ“Ί Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools

πŸ’¬ Thoughts? Share in the comments below!

Comments