Skip to main content
In this section, we will see how to build a basic stateless AI agent using ChatOpenAI and LangGraph.

Objectives

  1. Define the State schema for storing message history.
  2. Initialize ChatOpenAI and invoke it from a graph node.
  3. Build a stateless agent graph with a single processor node.
  4. Compile and run the agent.

Agent I: Simple Agent Bot

Goal

Build a simple stateless agent using LangGraph that takes user messages and passes them directly to an LLM for processing.

Sample Input

Sample Output

Plan

  1. Define the state schema (AgentState) containing messages (a list of messages).
  2. Define the node function process that invokes the LLM with the messages.
  3. Build the graph by adding the node and connecting START to it and it to END.
  4. Compile and invoke the agent.

Code Implementation

1. Define the State Schema

We define a schema AgentState with a list of messages:

2. Define the Node Function

The node function receives the state, invokes the model, and prints the response:

3. Build and Compile the Graph

We setup the StateGraph, register the node, and link entry and finish points:

4. Invoke the Agent

We invoke the compiled agent:

Exercise: Pirate Speak Translator Agent 🏴‍☠️

Goal

Build a custom system prompt agent that translates whatever user inputs into dramatic Pirate Speak.

Sample Input

Sample Output

Plan

  1. Reuse or create an AgentState containing the message list.
  2. Write a node function pirate_node that inserts a SystemMessage specifying the pirate persona ahead of the user messages, invokes the LLM, and prints the pirate response.
  3. Initialize the StateGraph, register the pirate node, hook it to START/END, and compile.
  4. Test the compiled agent with a human message and verify the pirate speak output.

Practice & Exercises

To reinforce what you’ve learned in this section, practice with the interactive notebook:

Practice & Exercises

Practice defining simple agent structures, customizing system instructions, and compiling basic workflows.💻 VS Code | 🚀 Colab | 📥 Download