Build With Abdallah logo Build With Abdallah Software · AI · Automation
Tutorial 8 min read Jun 07, 2026

Developing Custom AI Agents with LangChain and OpenAI API

In this tutorial, we will explore how to develop custom AI agents using LangChain and the OpenAI API. LangChain provides a framework for building applications powered by language m

A
Abdallah Mohamed
Senior Full-Stack Engineer
Developing Custom AI Agents with LangChain and OpenAI API

Developing Custom AI Agents with LangChain and OpenAI API

In this tutorial, we will explore how to develop custom AI agents using LangChain and the OpenAI API. LangChain provides a framework for building applications powered by language models, making it easier to create custom AI solutions. By leveraging the OpenAI API, we can harness the power of advanced language models to build intelligent agents capable of performing a wide range of tasks. This tutorial will guide you through setting up the necessary environment, structuring your project, and implementing a basic AI agent.

Prerequisites

Before we begin, ensure you have the following installed on your system:

  1. Python 3.8 or later: You can download it from python.org.
  2. pip: Python's package installer, which typically comes with Python.
  3. OpenAI Python Client: Install with pip.
  4. LangChain: Install with pip.

Run the following commands to install the necessary packages:

pip install openai
pip install langchain

Additionally, you will need an API key from OpenAI. You can obtain one by signing up at OpenAI's website.

Project Structure

We'll organize our project into a simple directory structure:

custom-ai-agent/
├── main.py
└── README.md

Step 1: Setting Up the OpenAI API Client

First, we need to set up the OpenAI API client to interact with the language models. Create a file named main.py in the custom-ai-agent directory.

# main.py

import openai

# Set up your OpenAI API key
openai.api_key = 'your-openai-api-key'

def ask_openai(prompt):
    response = openai.Completion.create(
        engine="davinci-codex",
        prompt=prompt,
        max_tokens=150
    )
    return response.choices[0].text.strip()

if __name__ == "__main__":
    prompt = "What is the capital of France?"
    answer = ask_openai(prompt)
    print(f"Answer: {answer}")

Explanation

  • Importing openai: We import the OpenAI library to interact with the OpenAI API.
  • API Key: Replace 'your-openai-api-key' with your actual API key from OpenAI.
  • ask_openai function: This function sends a prompt to the OpenAI API and returns the response.
  • Main block: We define a simple prompt and print the response from the API.

Step 2: Creating a Basic AI Agent

Next, we'll build a basic AI agent that can respond to user queries. We'll expand our main.py file to include a simple command-line interface.

# main.py

import openai

openai.api_key = 'your-openai-api-key'

def ask_openai(prompt):
    response = openai.Completion.create(
        engine="davinci-codex",
        prompt=prompt,
        max_tokens=150
    )
    return response.choices[0].text.strip()

def chat_with_agent():
    print("AI Agent: Hello! How can I assist you today?")
    while True:
        user_input = input("You: ")
        if user_input.lower() in ["exit", "quit"]:
            print("AI Agent: Goodbye!")
            break
        response = ask_openai(user_input)
        print(f"AI Agent: {response}")

if __name__ == "__main__":
    chat_with_agent()

Explanation

  • chat_with_agent function: This function provides a simple loop for user interaction. It allows the user to input queries and receive responses from the AI agent.
  • Exit condition: The loop exits when the user types "exit" or "quit".

Step 3: Enhancing the Agent with LangChain

To enhance our AI agent, we will integrate LangChain to manage interactions and responses more effectively. First, ensure LangChain is installed (refer to the Prerequisites section).

Now, modify main.py to include LangChain:

# main.py

import openai
from langchain import ConversationChain

openai.api_key = 'your-openai-api-key'

def ask_openai(prompt):
    response = openai.Completion.create(
        engine="davinci-codex",
        prompt=prompt,
        max_tokens=150
    )
    return response.choices[0].text.strip()

def chat_with_agent():
    conversation = ConversationChain()
    print("AI Agent: Hello! How can I assist you today?")
    while True:
        user_input = input("You: ")
        if user_input.lower() in ["exit", "quit"]:
            print("AI Agent: Goodbye!")
            break
        conversation.add_user_input(user_input)
        response = ask_openai(conversation.get_combined_input())
        conversation.add_agent_response(response)
        print(f"AI Agent: {response}")

if __name__ == "__main__":
    chat_with_agent()

Explanation

  • Importing ConversationChain: We import ConversationChain from LangChain to manage the conversation context.
  • Conversation management: ConversationChain helps in maintaining the flow of conversation by keeping track of user inputs and agent responses.

This setup provides a foundation for building more complex AI agents by leveraging the capabilities of LangChain and OpenAI's language models. In the next part of this tutorial, we will explore further enhancements and integrations.

Step 4: Adding Memory to the AI Agent

To make our AI agent more interactive and context-aware, we can add memory capabilities. This will allow the agent to remember past interactions during a session. LangChain provides the necessary tools to implement memory in our conversation chain.

First, update main.py to include memory:

# main.py

import openai
from langchain import ConversationChain
from langchain.memory import SimpleMemory

openai.api_key = 'your-openai-api-key'

def ask_openai(prompt):
    response = openai.Completion.create(
        engine="davinci-codex",
        prompt=prompt,
        max_tokens=150
    )
    return response.choices[0].text.strip()

def chat_with_agent():
    memory = SimpleMemory()
    conversation = ConversationChain(memory=memory)
    print("AI Agent: Hello! How can I assist you today?")
    while True:
        user_input = input("You: ")
        if user_input.lower() in ["exit", "quit"]:
            print("AI Agent: Goodbye!")
            break
        conversation.add_user_input(user_input)
        response = ask_openai(conversation.get_combined_input())
        conversation.add_agent_response(response)
        print(f"AI Agent: {response}")

if __name__ == "__main__":
    chat_with_agent()

Explanation

  • Importing SimpleMemory: We import SimpleMemory from LangChain to enable memory management.
  • Memory in ConversationChain: By passing a memory object to ConversationChain, the agent can retain information across interactions.

Step 5: Handling Errors and Logging

To make our AI agent more reliable, it's important to handle potential errors and log interactions for debugging purposes. We'll add basic error handling and logging to main.py.

# main.py

import openai
from langchain import ConversationChain
from langchain.memory import SimpleMemory
import logging

openai.api_key = 'your-openai-api-key'

# Set up logging
logging.basicConfig(level=logging.INFO)

def ask_openai(prompt):
    try:
        response = openai.Completion.create(
            engine="davinci-codex",
            prompt=prompt,
            max_tokens=150
        )
        return response.choices[0].text.strip()
    except Exception as e:
        logging.error(f"Error communicating with OpenAI: {e}")
        return "I'm sorry, I couldn't process that request."

def chat_with_agent():
    memory = SimpleMemory()
    conversation = ConversationChain(memory=memory)
    logging.info("Starting new chat session.")
    print("AI Agent: Hello! How can I assist you today?")
    while True:
        user_input = input("You: ")
        if user_input.lower() in ["exit", "quit"]:
            print("AI Agent: Goodbye!")
            logging.info("Ending chat session.")
            break
        conversation.add_user_input(user_input)
        response = ask_openai(conversation.get_combined_input())
        conversation.add_agent_response(response)
        print(f"AI Agent: {response}")

if __name__ == "__main__":
    chat_with_agent()

Explanation

  • Logging Setup: We configure basic logging to track interactions and errors.
  • Error Handling: The ask_openai function now includes a try-except block to handle exceptions from the OpenAI API.

Complete Working Example

Below is the complete main.py file with all enhancements:

# main.py

import openai
from langchain import ConversationChain
from langchain.memory import SimpleMemory
import logging

openai.api_key = 'your-openai-api-key'

# Set up logging
logging.basicConfig(level=logging.INFO)

def ask_openai(prompt):
    try:
        response = openai.Completion.create(
            engine="davinci-codex",
            prompt=prompt,
            max_tokens=150
        )
        return response.choices[0].text.strip()
    except Exception as e:
        logging.error(f"Error communicating with OpenAI: {e}")
        return "I'm sorry, I couldn't process that request."

def chat_with_agent():
    memory = SimpleMemory()
    conversation = ConversationChain(memory=memory)
    logging.info("Starting new chat session.")
    print("AI Agent: Hello! How can I assist you today?")
    while True:
        user_input = input("You: ")
        if user_input.lower() in ["exit", "quit"]:
            print("AI Agent: Goodbye!")
            logging.info("Ending chat session.")
            break
        conversation.add_user_input(user_input)
        response = ask_openai(conversation.get_combined_input())
        conversation.add_agent_response(response)
        print(f"AI Agent: {response}")

if __name__ == "__main__":
    chat_with_agent()

Common Errors and Fixes

  1. Invalid API Key: If you receive an authentication error, ensure your API key is correct and active.

    Fix: Double-check your API key and ensure it's correctly set in openai.api_key.

  2. Network Errors: If the agent fails to connect to the OpenAI API, check your internet connection.

    Fix: Ensure your network is stable and retry the request.

  3. Rate Limit Exceeded: If you hit the API rate limit, you may need to wait or upgrade your OpenAI plan.

    Fix: Monitor your API usage and consider optimizing requests or upgrading your plan.

Conclusion

In this tutorial, we developed a custom AI agent using LangChain and the OpenAI API. We structured our project, implemented a basic agent, enhanced it with memory, and added error handling and logging. This foundation allows you to build more sophisticated AI solutions tailored to your needs.

Sources