Building an Agent Workflow with LangChain and OpenAI: A Comprehensive Guide
Intelligent agents powered by large language models (LLMs) can autonomously perform tasks, make decisions, and interact with external tools, making them ideal for complex workflows like research, automation, or customer support. By leveraging LangChain and OpenAI, you can build a versatile agent workflow that combines reasoning, tool usage, and context awareness.
Introduction to Agent Workflows and LangChain
An agent workflow involves an LLM reasoning through tasks, selecting appropriate tools, and generating responses based on user input and external data. Unlike simple chatbots, agents can execute multi-step processes, such as searching the web, querying databases, or performing calculations. LangChain facilitates this with agent frameworks, tool integrations, and memory management. OpenAI’s API, powering models like gpt-3.5-turbo, drives the reasoning, while LangChain orchestrates the workflow.
This tutorial assumes basic Python knowledge, with references to LangChain’s getting started guide, OpenAI’s API documentation, and SerpAPI documentation.
Prerequisites for Building the Agent Workflow
Ensure you have:
- Python 3.8+: Download from python.org.
- OpenAI API Key: Obtain from OpenAI’s platform. Secure it per LangChain’s security guide.
- SerpAPI Key: Obtain from SerpAPI for web search integration.
- Python Libraries: Install langchain, openai, langchain-openai, langchain-community, and serpapi via:
pip install langchain openai langchain-openai langchain-community serpapi
- Development Environment: Use a virtual environment, as detailed in LangChain’s environment setup guide.
- Basic Python Knowledge: Familiarity with syntax and package installation, with resources in Python’s documentation.
Step 1: Setting Up the Development Environment
Configure your environment by importing libraries and setting API keys.
import os
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from langchain.tools import Tool
from langchain_community.utilities import SerpAPIWrapper
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
# Set API keys
os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["SERPAPI_API_KEY"] = "your-serpapi-key"
Replace "your-openai-api-key" and "your-serpapi-key" with your actual keys. Environment variables enhance security, as explained in LangChain’s security and API keys guide. The imported modules are core to the agent workflow, detailed in LangChain’s core components overview.
Step 2: Initializing the Language Model
Initialize the OpenAI LLM using ChatOpenAI for agent reasoning.
llm = ChatOpenAI(
model_name="gpt-3.5-turbo",
temperature=0.5,
max_tokens=512,
top_p=0.9,
frequency_penalty=0.1,
presence_penalty=0.1,
n=1
)
Key Parameters for ChatOpenAI
- model_name: OpenAI model (e.g., gpt-3.5-turbo, gpt-4). gpt-3.5-turbo is efficient; gpt-4 excels in complex reasoning. See OpenAI’s model documentation.
- temperature (0.0–2.0): Controls randomness. At 0.5, balances creativity and focus for agent decisions.
- max_tokens: Maximum response length (e.g., 512). Adjust for detail vs. cost. See LangChain’s token limit handling.
- top_p (0.0–1.0): Nucleus sampling. At 0.9, focuses on likely tokens.
- frequency_penalty (–2.0–2.0): Discourages repetition. At 0.1, promotes variety.
- presence_penalty (–2.0–2.0): Encourages new topics. At 0.1, slight novelty boost.
- n: Number of responses (e.g., 1). Single response suits agent tasks.
For alternatives, see LangChain’s integrations.
Step 3: Setting Up Tools for the Agent
Define tools the agent can use, such as a web search tool via SerpAPI and a custom calculator tool.
# Web search tool
search = SerpAPIWrapper(
serpapi_api_key=os.environ["SERPAPI_API_KEY"],
search_engine="google"
)
# Custom calculator tool
def calculator(expression: str) -> str:
try:
return str(eval(expression, {"__builtins__": {}}, {}))
except Exception as e:
return f"Error: {str(e)}"
tools = [
Tool(
name="WebSearch",
func=search.run,
description="Search the web for current information or trends."
),
Tool(
name="Calculator",
func=calculator,
description="Evaluate mathematical expressions (e.g., '2 + 3 * 4')."
)
]
Key Parameters for SerpAPIWrapper
- serpapi_api_key: API key for SerpAPI.
- search_engine: Search provider (e.g., "google"). Others include "bing".
Key Parameters for Tool
- name: Tool identifier (e.g., "WebSearch").
- func: Function to execute (e.g., search.run).
- description: Guides the agent on when to use the tool.
For more tools, see LangChain’s tools guide.
Step 4: Initializing the Agent
Create an agent using LangChain’s initialize_agent to combine the LLM, tools, and reasoning strategy.
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
max_iterations=5,
early_stopping_method="force",
handle_parsing_errors=True
)
Key Parameters for initialize_agent
- tools: List of available tools.
- llm: The initialized LLM.
- agent: Agent type (e.g., AgentType.ZERO_SHOT_REACT_DESCRIPTION). Uses ReAct (Reasoning and Acting) for zero-shot tasks.
- verbose: If True, logs agent decisions for debugging.
- max_iterations: Limits reasoning steps (e.g., 5) to prevent infinite loops.
- early_stopping_method: Stops execution (e.g., "force") if max_iterations reached.
- handle_parsing_errors: If True, gracefully handles tool output errors.
For advanced agents, see LangChain’s agent integration guide.
Step 5: Testing the Agent Workflow
Test the agent by running tasks that require reasoning and tool usage.
# Example task
task = "Find recent trends in AI development and calculate the average of 10, 20, and 30."
response = agent.run(task)
print("Response:", response)
Example Output:
Response: Recent trends in AI development include advancements in generative AI, increased focus on ethical AI frameworks, and growth in edge AI for IoT devices, as found via web search. The average of 10, 20, and 30 is (10 + 20 + 30) / 3 = 20.
Another task:
task = "What’s the latest news on renewable energy, and what’s 15% of 200?"
response = agent.run(task)
print("Response:", response)
Example Output:
Response: Recent news on renewable energy highlights breakthroughs in solar panel efficiency and offshore wind projects, per web search results. 15% of 200 is 0.15 * 200 = 30.
The agent uses SerpAPI for web searches and the calculator for math, demonstrating multi-step reasoning. For more examples, see LangChain’s agent examples.
Step 6: Customizing the Agent Workflow
Enhance the agent with conversational memory, custom prompts, or additional tools.
6.1 Adding Conversational Memory
Incorporate ConversationBufferMemory for context-aware interactions.
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True,
k=3
)
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
verbose=True,
memory=memory,
max_iterations=5,
early_stopping_method="force"
)
# Test with context
response = agent.run("Search for AI trends.")
print(response)
response = agent.run("Now focus on ethical AI.")
print(response)
ConversationBufferMemory Parameters:
- memory_key: History variable (default: "chat_history").
- return_messages: If True, returns message objects.
- k: Limits stored interactions (e.g., 3).
Example Output:
Advancements in generative AI and edge AI are prominent trends, per web search.
Ethical AI trends emphasize transparency, bias mitigation, and regulatory frameworks, building on prior AI trends.
See LangChain’s memory integration guide.
6.2 Custom Prompt Engineering
Modify the agent’s prompt for specific behavior.
custom_prompt = PromptTemplate(
input_variables=["chat_history", "input", "agent_scratchpad"],
template="You are a research assistant. Use tools and history to provide concise, accurate answers.\n\nHistory: {chat_history}\n\nInput: {input}\n\nScratchpad: {agent_scratchpad}\n\nResponse: ",
validate_template=True
)
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
agent_kwargs={"prompt": custom_prompt},
max_iterations=5
)
PromptTemplate Parameters:
- input_variables: Variables (e.g., ["chat_history", "input", "agent_scratchpad"]).
- template: Defines tone and structure.
- validate_template: If True, validates variables.
See LangChain’s prompt templates guide.
6.3 Adding More Tools
Integrate a database query tool using LangChain’s SQL chains.
from langchain_community.utilities import SQLDatabase
from langchain.chains import create_sql_query_chain
db = SQLDatabase.from_uri("sqlite:///sample.db")
sql_chain = create_sql_query_chain(llm=llm, db=db)
tools.append(
Tool(
name="DatabaseQuery",
func=lambda q: sql_chain.invoke({"question": q}),
description="Query a SQLite database."
)
)
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
max_iterations=5
)
Test with:
response = agent.run("Query employees hired in 2023 from the database.")
print(response)
See LangChain’s SQL database chains.
Step 7: Deploying the Agent Workflow
Deploy as a Streamlit app for an interactive interface.
import streamlit as st
st.title("Intelligent Agent Workflow")
st.write("Interact with an AI agent that uses tools and reasoning!")
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Enter your task:"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Processing..."):
response = agent.run(prompt)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
Save as app.py, install Streamlit (pip install streamlit), and run:
streamlit run app.py
Visit http://localhost:8501. Deploy to Streamlit Community Cloud by pushing to GitHub and configuring secrets. See LangChain’s Streamlit tutorial or Streamlit’s deployment guide.
Step 8: Evaluating and Testing the Agent
Evaluate responses using LangChain’s evaluation metrics.
from langchain.evaluation import load_evaluator
evaluator = load_evaluator(
"qa",
criteria=["correctness", "relevance"]
)
result = evaluator.evaluate_strings(
prediction="Generative AI and ethical frameworks are trending.",
input="What are AI trends?",
reference="Recent AI trends include generative AI and ethical considerations."
)
print(result)
load_evaluator Parameters:
- evaluator_type: Metric type (e.g., "qa").
- criteria: Evaluation criteria.
Test with diverse tasks (e.g., “Search for renewable energy news and calculate 25% of 400”). Debug with LangSmith per LangChain’s LangSmith intro.
Advanced Features and Next Steps
Enhance with:
- Complex Workflows: Use LangGraph for multi-step flows.
- Enterprise Use Cases: Explore LangChain’s enterprise examples.
- Custom Tools: Build tools for specific tasks per LangChain’s tools guide.
- Memory Enhancements: Use LangChain’s memory integration.
See LangChain’s startup examples or GitHub repos.
Conclusion
Building an agent workflow with LangChain and OpenAI enables intelligent, tool-driven task execution. This guide covered setup, tool integration, agent creation, deployment, evaluation, and parameters. Leverage LangChain’s agents, tools, and integrations to create versatile workflows.
Explore chains, memory, or evaluation metrics. Debug with LangSmith. Happy coding!