Skip to main content

Overview

The LangchainAgent bridges AG-Kit with LangChain’s createAgent runtime. It reuses the LanggraphAgent event model under the hood (compiledWorkflow = agent), so it streams the same BaseEvent types and works seamlessly with @ag-kit/server. Key capabilities:
  • Wraps a LangChain agent created via createAgent
  • Streams AG-UI compatible events (text chunks, tool calls, results)
  • Adds client-supplied tools to model calls via the agKitClientTools middleware

Installation

pnpm add @ag-kit/agents @ag-kit/adapter-langchain langchain @langchain/openai @langchain/langgraph

Exports

All exports are available from @ag-kit/adapter-langchain: Exports:
  • LangchainAgent – Main agent class for LangChain integration
  • agKitClientTools – Middleware to inject client tools into LangChain model calls
  • convert2LangChain – Convert an AG‑Kit BaseTool to a LangChain DynamicStructuredTool
  • convertLangChain2AGKit – Convert a LangChain StructuredTool to an AG‑Kit Dynamic Tool

Quick start

import { createAgent as createLangchainAgent } from "langchain";
import { MemorySaver } from "@langchain/langgraph";
import { ChatOpenAI } from "@langchain/openai";
import { LangchainAgent, agKitClientTools } from "@ag-kit/adapter-langchain";

const checkpointer = new MemorySaver();

// 1) Create LangChain agent
const lcAgent = createLangchainAgent({
  model: new ChatOpenAI({
    model: process.env.OPENAI_MODEL!,
    apiKey: process.env.OPENAI_API_KEY!,
    configuration: { baseURL: process.env.OPENAI_BASE_URL! },
  }),
  checkpointer,
  middleware: [agKitClientTools()],
});

// 2) Wrap with AG‑Kit agent
export const agent = new LangchainAgent({
  agent: lcAgent,
  name: "agentic-chat-agent",
  description: "A helpful AI assistant",
});

Server integration

import { run } from "@ag-kit/server";
import { agent } from "./agent";

run({ createAgent: () => ({ agent }) });

Using AG‑Kit tools with LangChain

agKitClientTools makes your client-provided tools available to the underlying LangChain model call (request.tools). Any tools passed to agent.run(…) will be appended as DynamicStructuredTool definitions.
import { agKitClientTools } from "@ag-kit/adapter-langchain";
import { createAgent as createLangchainAgent } from "langchain";

const agent = createLangchainAgent({
  model, checkpointer, middleware: [agKitClientTools()],
});

API

class LangchainAgent

new LangchainAgent(config: AgentConfig & {
  agent: ReturnType<typeof createAgent>;
})
name
string
required
Unique identifier for the agent.
description
string
Human-readable description of the agent.
agent
ReturnType<typeof createAgent>
required
LangChain agent instance returned by createAgent. It is passed to the base LanggraphAgent as compiledWorkflow.

Methods

run()
run(input: RunAgentInput): Observable<BaseEvent>
Executes the underlying workflow and emits BaseEvent values from @ag-ui/client.
  • input – RunAgentInput containing messages, runId, threadId, tools, etc.
  • returns – Observable<BaseEvent>
Note: Event types and semantics are identical to LanggraphAgent. See LangGraph Agent for the full event list.

agKitClientTools()

agKitClientTools(): Middleware
purpose
string
Adds client-provided tools from state.agKit.actions into LangChain’s request.tools for the model call.

Tool conversion helpers

convert2LangChain(agkitTool: BaseTool, impl?: (tool: BaseTool) => Function): DynamicStructuredTool
convertLangChain2AGKit(langchainTool: StructuredTool): DynamicTool
  • convert2LangChain – Wrap an AG‑Kit tool for use with LangChain
  • convertLangChain2AGKit – Wrap a LangChain tool for use with AG‑Kit

See also