LangChain.js: Building LLM Applications with TypeScript

Lesson 4 of 6

Tools and Agents in LangChain.js

A LangChain.js tool wraps a TypeScript function with a name, description, and typed schema so a model can request it, and an agent executor repeatedly calls the model, executes any requested tools, and feeds results back, until the model gives a final answer.

Defining a tool

TypeScript

The schema uses Zod, a TypeScript-first validation library, to describe exactly what arguments the tool accepts, LangChain.js converts that schema into the JSON Schema the model actually sees, and validates the model's arguments against it before your function ever runs, malformed input is rejected before it reaches your code, not after.

Binding tools to a model

TypeScript

This needs a real API key, so it's read-only here. .bindTools() returns a new model that can respond with a tool_calls array when it decides a tool is relevant, the model never runs getWordLength itself, it only requests that your code do so.

The agent executor

TypeScript

Read-only, needs a real model. AgentExecutor is the loop: call the model, check for tool_calls, run any requested tools, append their results, and repeat until the model returns a final plain-text answer instead of another tool request.

NOTE

A Zod schema does double duty here: it's what generates the tool's JSON Schema description for the model, and it's what validates the model's arguments before your function runs, one source of truth instead of two things to keep in sync by hand.

📝 Tools & Agents in TS Quiz

Passing score: 70%
  1. 1.What library does LangChain.js use to define a tool's argument schema?

  2. 2.LangChain.js validates a model's tool call arguments against the Zod schema before the tool function runs.

  3. 3.model.bindTools([...]) returns a new model that can respond with a ____ array when it decides to call a tool.