LangChain Tutorial (TypeScript): testing agents locally for intermediate developers

By Cyprian AaronsUpdated 2026-04-21
langchaintesting-agents-locally-for-intermediate-developerstypescript

This tutorial shows you how to run and test a LangChain agent locally in TypeScript without wiring it into your app yet. You need this when you want fast feedback on prompts, tool behavior, and agent decisions before pushing anything into a backend or production workflow.

What You'll Need

  • Node.js 18+
  • TypeScript 5+
  • npm or pnpm
  • An OpenAI API key
  • These packages:
    • langchain
    • @langchain/openai
    • @langchain/core
    • typescript
    • ts-node or tsx
    • dotenv

Step-by-Step

  1. Install the dependencies and set up your project. Keep this in a clean folder so you can isolate agent behavior from the rest of your application.
npm init -y
npm install langchain @langchain/openai @langchain/core dotenv
npm install -D typescript tsx @types/node
npx tsc --init
  1. Add your API key in a local .env file. For local testing, this is enough; do not hardcode keys into the agent file.
OPENAI_API_KEY=your_openai_api_key_here
  1. Create a simple tool the agent can call. For local testing, use something deterministic like a calculator-style function so you can verify whether the agent is choosing tools correctly.
import "dotenv/config";
import { tool } from "@langchain/core/tools";
import { z } from "zod";

export const multiplyTool = tool(
  async ({ a, b }) => {
    return `${a * b}`;
  },
  {
    name: "multiply",
    description: "Multiply two numbers together.",
    schema: z.object({
      a: z.number(),
      b: z.number(),
    }),
  }
);
  1. Build the agent with a chat model and bind the tool to it. This example uses the standard LangChain agent pattern with OpenAI function calling under the hood.
import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { HumanMessage } from "@langchain/core/messages";
import { multiplyTool } from "./multiplyTool";

const model = new ChatOpenAI({
  model: "gpt-4o-mini",
  temperature: 0,
});

const agent = createReactAgent({
  llm: model,
  tools: [multiplyTool],
});

async function main() {
  const result = await agent.invoke({
    messages: [new HumanMessage("What is 12 times 9? Use the tool.")],
  });

  console.log(JSON.stringify(result, null, 2));
}

main().catch(console.error);
  1. Run the script locally and inspect the output. You want to see whether the assistant calls multiply or tries to answer from memory.
npx tsx src/agent.ts
  1. Add a tiny test harness so you can repeat checks without manually reading logs every time. This gives you a stable way to validate prompt changes and tool wiring.
import "dotenv/config";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { HumanMessage } from "@langchain/core/messages";
import { multiplyTool } from "./multiplyTool";

const model = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 });
const agent = createReactAgent({ llm: model, tools: [multiplyTool] });

async function testAgent(input: string, expectedSubstring: string) {
  const result = await agent.invoke({ messages: [new HumanMessage(input)] });
  const text = JSON.stringify(result);
  console.log(input);
  console.log(text.includes(expectedSubstring) ? "PASS" : "FAIL");
}

await testAgent("What is 6 times 7? Use the tool.", "42");
await testAgent("Multiply 8 and 5.", "40");

Testing It

Run the agent with a few prompts that should clearly trigger the tool, like “What is 6 times 7?” or “Multiply 8 and 5.” If the output contains the right number and you see evidence that the tool was used, your wiring is correct.

Then try prompts that should not need the tool, such as “Explain what this agent does.” That helps you confirm whether the model is overusing tools or staying within scope.

If you want stronger validation, wrap those checks in a real test runner like Vitest and assert on structured outputs instead of raw strings. That makes regressions much easier to catch when you change prompts or swap models.

Next Steps

  • Add more tools with strict Zod schemas for bank-style workflows like policy lookup or transaction classification.
  • Replace ad hoc logging with Vitest tests that assert on tool calls and final messages.
  • Learn LangGraph state handling so you can test multi-step agents locally before deploying them behind an API.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides