Getting Started
In this guide, we’ll build a command-line AI assistant that can read its own wallet balance and transfer tokens using OpenAI, LangChain, and Agentkit.
Prerequisites
Section titled “Prerequisites”- Node.js installed
- An OpenAI API Key (
OPENAI_API_KEY) - A 0xGasless API Key (
0XGASLESS_API_KEY) from the Dashboard
1. Setup
Section titled “1. Setup”Create a new directory and install the necessary packages.
mkdir my-agentcd my-agentnpm init -ynpm install @0xgasless/agent-sdk @langchain/openai @langchain/corenpm install dotenvCreate a .env file in the root of your project:
OPENAI_API_KEY="sk-..."0XGASLESS_API_KEY="your-0xgasless-api-key"AGENT_PRIVATE_KEY="0x..." # Optional: Provide an existing private key, or let the SDK generate one2. Initialize the Agent
Section titled “2. Initialize the Agent”Create an index.ts file. We will initialize the core AgentKit instance.
import { AgentKit } from "@0xgasless/agent-sdk";import 'dotenv/config';
async function main() { // 1. Initialize the core AgentKit // This creates (or loads) the agent's wallet and connects to the paymaster/bundler. const agentKit = new AgentKit({ apiKey: process.env['0XGASLESS_API_KEY']!, privateKey: process.env.AGENT_PRIVATE_KEY, // Uses a random key if omitted chainId: 11155111, // Sepolia testnet });
const walletAuth = await agentKit.getWalletAuth(); console.log("Agent's Wallet Address:", await walletAuth.getAddress());3. Connect LangChain
Section titled “3. Connect LangChain”Now we inject the 0xGasless tools into a LangChain agent. We use a pre-built prompt that tells the LLM it has access to a crypto wallet.
import { getLangChainTools } from "@0xgasless/agent-sdk/langchain"; import { ChatOpenAI } from "@langchain/openai"; import { createReactAgent } from "@langchain/langgraph/prebuilt"; import { MemorySaver } from "@langchain/langgraph";
// 2. Wrap AgentKit tools into LangChain format const tools = getLangChainTools(agentKit);
// 3. Initialize the LLM const model = new ChatOpenAI({ modelName: "gpt-4o-mini", temperature: 0, });
// 4. Create the ReAct agent const agent = createReactAgent({ llm: model, tools: tools, checkpointSaver: new MemorySaver(), });4. Chat with the Agent
Section titled “4. Chat with the Agent”Finally, let’s send a natural language prompt to the agent.
console.log("Sending prompt to agent...");
const config = { configurable: { thread_id: "Session1" } };
const stream = await agent.stream( { messages: [["user", "What is your wallet address and native balance?"]] }, config );
for await (const chunk of stream) { if ("agent" in chunk) { console.log("Agent:", chunk.agent.messages[0].content); } else if ("tools" in chunk) { console.log("[Tool Execution]:", chunk.tools.messages[0].name); } }}
main().catch(console.error);5. Run the Agent
Section titled “5. Run the Agent”Run the script. The agent will read your prompt, realize it needs to use the GetAddressAction and GetBalanceAction tools, execute them on-chain, and return the result to you in simple English.
npx ts-node index.tsOutput:
Agent's Wallet Address: 0x...Sending prompt to agent...[Tool Execution]: get_address[Tool Execution]: get_balanceAgent: My wallet address is 0x... and my native balance is currently 0 ETH.Next Steps
Section titled “Next Steps”- Want to see how the tools actually format data? Read How Agents Work.
- Need to persist the agent’s wallet across restarts? Read Wallet Management.