ERWAN.TECH

Getting Started with AI Agents Using LangChain

A beginner-friendly introduction to building AI agents with LangChain and LangGraph in TypeScript.

ai

What are AI agents?

An AI agent is more than a chatbot. It can reason, use tools, and take actions to accomplish tasks. Think of it as an LLM with superpowers.

Instead of just answering questions, an agent can:

  • Search the web for information
  • Query a database
  • Call APIs
  • Write and execute code

Why LangChain?

LangChain provides the building blocks for creating AI-powered applications. Combined with LangGraph, you can build complex agent workflows with clear state management.

The TypeScript ecosystem has matured a lot — here's how to get started.

Setting up

First, install the dependencies:

npm install langchain @langchain/openai @langchain/langgraph

A simple agent

Here's a basic agent that can search the web and answer questions:

import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
 
const model = new ChatOpenAI({
  modelName: "gpt-4o",
  temperature: 0,
});
 
const tools = [new TavilySearchResults({ maxResults: 3 })];
 
const agent = createReactAgent({
  llm: model,
  tools,
});
 
const result = await agent.invoke({
  messages: [
    {
      role: "user",
      content: "What's the latest news about Next.js?",
    },
  ],
});

The agent will automatically decide when to search and when to respond directly.

Adding custom tools

The real power comes from custom tools. Let's create one that queries a database:

import { tool } from "@langchain/core/tools";
import { z } from "zod";
 
const getUser = tool(
  async ({ email }) => {
    const user = await db.users.findUnique({
      where: { email },
    });
    return JSON.stringify(user);
  },
  {
    name: "get_user",
    description: "Look up a user by their email address",
    schema: z.object({
      email: z.string().email(),
    }),
  }
);

Now the agent can look up users when asked questions like "What plan is john@example.com on?"

State management with LangGraph

For more complex workflows, LangGraph lets you define explicit states and transitions:

  1. Nodes — individual steps (call LLM, use tool, check condition)
  2. Edges — connections between steps
  3. State — shared data that flows through the graph

This is perfect for multi-step processes like customer support bots, data pipelines, or research assistants.

What I'm building with this

I used this approach to build Feels, an AI messaging coach. The agent analyzes conversation context, detects emotional tone, and suggests responses — all powered by a LangGraph workflow.

Resources

If you want to dive deeper:

The AI agent space is moving fast. Start small, build a simple tool-using agent, and iterate from there.