Skip to content

Langchain Integration

If you are building an AI agent using LangChain or LangGraph, you don’t need to manually map JSON tool calls to Agentkit actions. We provide a single helper function to construct a list of LangChain-compatible DynamicStructuredTool objects.

The @0xgasless/agent-sdk/langchain module exports getLangChainTools().

import { AgentKit } from "@0xgasless/agent-sdk";
import { getLangChainTools } from "@0xgasless/agent-sdk/langchain";
// 1. Initialize AgentKit with the user's keys
const agentKit = new AgentKit({
apiKey: "YOUR_API_KEY",
});
// 2. Generate the tools list
const tools = getLangChainTools(agentKit);

getLangChainTools returns an array of DynamicStructuredTool[]. This is LangChain’s native class for representing function tools.

Each tool contains:

  1. name: E.g., smart_transfer
  2. description: A detailed instructional string telling the LLM exactly when to use this tool, and what the formatting constraints are. (e.g., “Use this tool to transfer ERC20 tokens or Native Assets…”).
  3. schema: A Zod schema defining the expected arguments.
  4. func: The executable callback that runs the specific AgentKit action (e.g., executing SmartTransferAction.invoke).

By default, getLangChainTools returns every action available in Agentkit. Sometimes you want an agent to be restricted — for example, a “Balance Bot” that can only read balances but cannot transfer funds.

You can restrict the generated tools array using standard JavaScript array filtering.

import { GetBalanceAction, GetAddressAction } from "@0xgasless/agent-sdk";
const allTools = getLangChainTools(agentKit);
// Keep only read-only actions
const readOnlyTools = allTools.filter(tool => {
return (
tool.name === GetBalanceAction.name ||
tool.name === GetAddressAction.name
);
});
// Now inject readOnlyTools into your LangGraph agent

Every action in Agentkit exports a static .name property, which corresponds directly to the LangChain tool name string emitted by getLangChainTools.

Once you have the tools array, inject it into the createReactAgent constructor (or any standard LangChain bind):

import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
const model = new ChatOpenAI({
modelName: "gpt-4o",
});
const agent = createReactAgent({
llm: model,
tools: tools, // Array of LangChain tools injected
});