Typescript bindings for langchain
LangChain is a TypeScript framework for building applications powered by large language models (LLMs). It provides a unified interface for integrating models from OpenAI, Anthropic, Google Gemini, and other providers, abstracting away provider-specific APIs into consistent patterns. With over 1.5 million weekly downloads, it has become the de facto standard for LLM orchestration in JavaScript environments.
The framework emerged from the need to handle common LLM workflows beyond simple API calls: chaining multiple operations together, managing conversation memory, integrating external tools, and implementing retrieval-augmented generation (RAG). Instead of writing custom logic for each provider and managing state manually, developers use LangChain's modular components to compose complex behaviors declaratively.
LangChain's architecture separates concerns into discrete NPM packages: @langchain/core provides base abstractions, while provider-specific packages like @langchain/openai or @langchain/anthropic offer implementations. The companion LangGraph library handles stateful agent execution with features like human-in-the-loop approval, persistent memory across sessions, and streaming responses. Version 1.x introduced production-ready features including middleware for PII redaction, structured output parsing with Zod schemas, and LangSmith integration for observability.
The framework targets teams building conversational interfaces, document analysis systems, autonomous agents, and data enrichment pipelines. While the abstraction layer adds complexity for trivial use cases, it becomes invaluable when applications need to switch providers, coordinate multiple model calls, or maintain conversational context across user sessions.
import { ChatOpenAI } from '@langchain/openai';
import { z } from 'zod';
import { tool } from '@langchain/core/tools';
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents';
import { ChatPromptTemplate } from '@langchain/core/prompts';
// Define a tool with typed schema
const weatherTool = tool(
async ({ location }) => {
// Simulate API call
const conditions = ['sunny', 'cloudy', 'rainy'];
return `Weather in ${location}: ${conditions[Math.floor(Math.random() * conditions.length)]}, 72°F`;
},
{
name: 'get_weather',
description: 'Get current weather for a location',
schema: z.object({
location: z.string().describe('City name')
})
}
);
const model = new ChatOpenAI({
modelName: 'gpt-4',
temperature: 0
});
const prompt = ChatPromptTemplate.fromMessages([
['system', 'You are a helpful assistant with access to weather data.'],
['human', '{input}'],
['placeholder', '{agent_scratchpad}']
]);
const agent = createToolCallingAgent({
llm: model,
tools: [weatherTool],
prompt
});
const executor = new AgentExecutor({
agent,
tools: [weatherTool]
});
const result = await executor.invoke({
input: 'What is the weather like in San Francisco and should I bring an umbrella?'
});
console.log(result.output);Retrieval-Augmented Generation (RAG) Systems: Combine document embeddings with LLM queries to answer questions from private knowledge bases. LangChain handles document loading, vector store integration (Pinecone, Weaviate), and orchestrating retrieval before generation.
Multi-Step Research Agents: Build agents that autonomously decompose tasks, call external APIs (weather, databases, search engines), and synthesize results. The LangGraph extension manages agent state and tool execution loops with automatic retries and error handling.
Conversational Chatbots with Memory: Maintain conversation history across sessions using built-in memory managers. Middleware can automatically summarize long conversations to stay within token limits while preserving context.
Structured Data Extraction: Parse unstructured text into typed objects using Zod schemas and tool calling. The framework handles validation, retries on malformed output, and provider-specific quirks in structured output formats.
Content Moderation Pipelines: Chain together multiple models for tasks like PII detection, sentiment analysis, and content classification. Built-in middleware supports redaction and logging before responses reach end users.
AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript
<p align="center"> <img height="100" width="100" alt="LlamaIndex logo" src="https://ts.llamaindex.ai/square.svg" /> </p> <h1 align="center">LlamaIndex.TS</h1> <h3 align="center"> Data framework for your LLM application. </h3>
The official TypeScript library for the OpenAI API
npm install langchainpnpm add langchainbun add langchain