In this in-depth tutorial, you’ll learn exactly how to build an AI agent from scratch using Python and LangChain. We’ll cover key tools, structured output, and best practices, all tailored for advanced developers. This guide also shows how AI agents can fit into enterprise data strategies—an area where B EYE excels—ensuring you can scale your solutions seamlessly across global teams and data environments.
See B EYE’s GenAI Services
Before exploring how to build an AI agent, let’s outline the essential technologies and why this matters for broader enterprise solutions:
- Python 3.10 or Above: While other versions may work, Python 3.10+ is recommended.
- LangChain: A popular framework that simplifies agent orchestration, giving your AI the ability to call “tools” such as search utilities or custom Python functions.
- Virtual Environment & Pip: We assume you’re comfortable creating virtual environments and installing packages.
- LLM Provider: You’ll need an API key from OpenAI (GPT family) or Anthropic (Claude family), among other potential large language model (LLM) providers.
- GitHub Copilot or Similar (Optional): Generative AI coding tools, like Microsoft Copilot, can significantly speed up coding tasks.
At B EYE, we often integrate such AI agents into end-to-end data pipelines, leveraging our expertise in data management, analytics, and enterprise performance management. Whether you’re using this tutorial to prototype a new product or to enhance your organization’s data strategy, the steps below will show you how to build an AI agent that fits seamlessly into a broader architectural vision.
Since you’re likely already comfortable with Python and package managers, here’s a straightforward rundown:
1. Create a New Project Folder
Example:
AI_Agent_Tutorial/
2. Set Up a Virtual Environment (Optional but Recommended)
python -m venv venv
# Windows activation
.\venv\Scripts\activate
# Mac/Linux activation
source venv/bin/activate
3. Create requirements.txt
Below is a sample requirements.txt you can copy. It includes LangChain and community tools like DuckDuckGo search and Wikipedia wrappers:
langchain==0.0.179
openai==0.27.4
anthropic==0.2.9
python-dotenv==1.0.0
pydantic==1.10.7
duckduckgo-search==2.9.5
(Adjust versions as needed.)
4. Install Dependencies
pip install -r requirements.txt
5. Obtain and Store API Keys
In the root directory, create a file named .env to store these keys securely:
OPENAI_API_KEY=YOUR_OPENAI_KEY ANTHROPIC_API_KEY=YOUR_ANTHROPIC_KEY
This setup ensures you have everything ready to start coding.
In this section, we’ll create a Python script that leverages LangChain to invoke an LLM and parse structured output.
1. Main File Structure
Create a file named main.py in your project folder:
main.py
from dotenv import load_dotenv from pydantic import BaseModel from langchain.chat_models import ChatOpenAI from langchain.chat_models import ChatAnthropic from langchain.prompts import ChatPromptTemplate from langchain.output_parsers import PydanticOutputParser from langchain.agents.agent_toolkits import create_tool_calling_agent from langchain.agents import AgentExecutor
1. Load environment variables
load_dotenv()
2. Select your LLM provider (OpenAI or Anthropic)
Uncomment the one you want to use:
llm = ChatOpenAI(
model="gpt-3.5-turbo",
openai_api_key=os.getenv("OPENAI_API_KEY")
)
Anthropic Claude model example:
llm = ChatAnthropic( model="claude-2", # or your preferred Claude version anthropic_api_key=os.getenv("ANTHROPIC_API_KEY") )
3. Define a response schema using Pydantic
class ResearchResponse(BaseModel): topic: str summary: str sources: list[str] tools_used: list[str]
Create a parser that uses the Pydantic model
parser = PydanticOutputParser(pydantic_object=ResearchResponse)
4. Build a prompt template
prompt = ChatPromptTemplate.from_messages([ ( "system", "You are a research assistant that will generate a research paper. " "Answer the user query, use the necessary tools, and format the output strictly in the schema provided. " "Wrap the response in this JSON format and provide no other text. " "Format:\n{format_instructions}" ), ("user", "{query}"), ("system", "Current conversation:\n{chat_history}\n\n{agent_scratchpad}") ]).partial(format_instructions=parser.get_format_instructions())
5. Create the Agent
Tools will be added later; for now, pass an empty list.
agent = create_tool_calling_agent( llm=llm, prompt=prompt, tools=[] )
agent_executor = AgentExecutor( agent=agent, tools=[], verbose=True # Set to False to hide the agent's thought process )
def run_agent(query: str): """Invoke the agent on a given query, parse the structured output, and return a Python object.""" raw_response = agent_executor.invoke({"query": query})
try:
# The agent returns a dictionary containing `output`, which has text
structured_response = parser.parse(raw_response["output"][0]["text"])
return structured_response
except Exception as e:
print("Error parsing response:", e)
print("Raw response:", raw_response)
return None
if name == "main": user_query = input("What can I help you research? ") result = run_agent(user_query) print(result)
Key Highlights
- ResearchResponse class: Defines the structure of the AI’s output (topic, summary, sources, and tools used).
- PydanticOutputParser: Enforces structured JSON output that is then easily parsed into a Python object.
- create_tool_calling_agent: Generates an agent that can (optionally) use external tools.
- AgentExecutor: Wraps the agent’s functionality to execute the prompt with verbose output if you need to debug or see the chain-of-thought.
Agents gain real power when they can call “tools” dynamically—for example, searching the web or fetching data from Wikipedia. Below, you’ll see how to build an AI agent that integrates multiple tools, including your own custom functions.
1. Create tools.py
In the same folder, create a file named tools.py:
tools.py
from langchain_community.tools.utilities import WikipediaAPIWrapper from langchain_community.tools.utilities import DuckDuckGoSearchRun from langchain.tools import Tool from datetime import datetime
1. Wrap DuckDuckGo search as a tool
search = DuckDuckGoSearchRun() search_tool = Tool( name="search", func=search.run, description="Search the web for information." )
2. Wrap Wikipedia lookup as a tool
wiki_api = WikipediaAPIWrapper(top_k_results=1, lang='en') wiki_tool = wiki_api.run # You can wrap in Tool directly if needed
3. Custom function to save data to a local text file
def save_to_txt(data: str, file_name: str) -> str: """ Saves the provided data into a text file with a timestamp. """ timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") full_name = f"{file_name}_{timestamp}.txt" with open(full_name, "w", encoding="utf-8") as f: f.write("Research Output\n\n") f.write(f"Timestamp: {timestamp}\n") f.write(f"{data}\n") return f"Data saved to {full_name}"
save_tool = Tool( name="save_text_to_file", func=save_to_txt, description="Save structured research data to a text file." )
2. Integrate Tools into main.py
Update main.py to import and use these tools:
main.py (append to existing code or update accordingly)
from tools import search_tool, wiki_tool, save_tool
...
agent = create_tool_calling_agent( llm=llm, prompt=prompt, tools=[search_tool, Tool(name="wikipedia", func=wiki_tool, description="Fetch info from Wikipedia."), save_tool] )
agent_executor = AgentExecutor( agent=agent, tools=[search_tool, Tool(name="wikipedia", func=wiki_tool, description="Fetch info from Wikipedia."), save_tool], verbose=True )
3. Run the Updated Agent
Now, when you run main.py, the agent can:
- Search the web
- Fetch from Wikipedia
- Save data to a local file
python main.py
# Enter your research query:
# e.g., "Tell me about LangChain and save the output to a file."
You should see the agent possibly calling both search and Wikipedia tools, then saving the final summary to a local .txt file.
Building an effective AI agent involves more than just coding the basic functionality. To ensure reliable performance and maintain a secure, enterprise-ready solution, it’s important to follow proven testing strategies and best practices. Below are several key considerations to keep in mind as you refine and scale your agent.
1. Error Handling
Notice the try/except block in the code to catch malformed outputs from the LLM. Consider adding retry mechanisms or more robust error logging in production.
2. Prompt Engineering
The system prompt is crucial. If your agent repeatedly produces off-target results, refine your instructions.
3. Security & Governance
In enterprise contexts, ensure you’re not exposing sensitive data to external APIs. B EYE regularly helps clients establish data governance frameworks to mitigate risk.
4. Scaling Tools
This tutorial uses DuckDuckGo and Wikipedia. In an enterprise solution, you might integrate internal databases, data warehouses, or proprietary APIs.
5. Performance Considerations
Each tool call increases latency. For large-scale systems, consider strategies like caching results or summarizing long text responses to reduce API calls.
You’ve just learned how to build an AI agent in Python, from installing dependencies and structuring your code, to adding external “tools” that expand the agent’s capabilities. This how to build an AI agent from scratch approach showcases how easy it is to start, yet how powerful these agents become when integrated with real-world data sources.
For organizations aiming to leverage advanced analytics and AI more holistically, B EYE’s expertise spans:
- Data Governance & Management: Ensuring secure, high-quality data pipelines.
- Analytics & EPM: Tying AI-driven insights to enterprise performance management for real-time decision-making.
- AI Integration: Building custom solutions that incorporate LLM agents into existing systems at scale.
When done right, AI agents can offload routine tasks, reduce manual effort, and free technical teams to focus on strategic innovations. Whether you’re prototyping a new AI-driven feature or planning a wide-scale enterprise deployment, how to build an AI agent is just the beginning of a broader journey into data-driven transformation.
Feel free to adapt, expand, or integrate this tutorial into your own codebase. With the right architectural vision and enterprise expertise, you can build robust, scalable AI agents that drive substantial business value. If you have questions or want help scaling this approach, reach out to B EYE to learn more about our end-to-end data solutions.
Happy coding—and welcome to the future of intelligent, tool-enabled AI!
Have AI Questions?
Let’s talk!
Ask an expert at +1 888 564 1235 (for US) or +359 2 493 0393 (for Europe) or fill in our form below to tell us more about your project.