The ICP Agent Kit includes a powerful LangChain integration that enables natural language interaction with the Internet Computer blockchain. This integration provides pre-built tools, specialized agents, and natural language processing capabilities.
Prerequisites: You’ll need an OpenAI API key to use the natural language features. Set it as an environment variable (OPENAI_API_KEY) or pass it in the configuration.

Architecture Overview

LangChain Architecture The LangChain integration consists of three main components:

1. Natural Language Processor

The NLP processor uses OpenAI’s GPT-4 to understand natural language commands and map them to the appropriate tools.

2. Tool System

10 pre-built LangChain tools that wrap the core plugin functionality:
  • Identity Tools: Create and manage identities
  • Token Tools: Transfer tokens and check balances
  • Canister Tools: Deploy and manage canisters
  • Cycles Tools: Monitor and top-up cycles

3. Specialized Agents

Purpose-built agents for different use cases:
  • General Agent: All-purpose blockchain operations
  • Developer Agent: Focused on canister development
  • DeFi Agent: Optimized for token operations
  • Governance Agent: NNS and DAO operations

Quick Start

Basic Setup

import { ICPAgent } from 'icp-agent-kit';

// Initialize with OpenAI
const agent = new ICPAgent({
  network: 'mainnet',
  openai: {
    apiKey: process.env.OPENAI_API_KEY,
    model: 'gpt-4-turbo-preview', // optional, this is the default
  },
});

await agent.initialize();

Natural Language Commands

Once initialized, you can use natural language to interact with the blockchain:
// Direct natural language processing
const result = await agent.processNaturalLanguage('Transfer 5 ICP to ryjl3-tyaaa-aaaaa-aaaba-cai');

// Check balance
const balance = await agent.processNaturalLanguage('What is my ICP balance?');

// Deploy a canister
const deployment = await agent.processNaturalLanguage(
  'Create a new canister with 2 trillion cycles',
);

Using Specialized Agents

For more focused interactions, use specialized agents:
// Create a DeFi agent for token operations
const defiAgent = agent.createAgent('defi');
const response = await defiAgent.chat('I want to transfer some tokens. What do I need to know?');

// Create a developer agent for canister work
const devAgent = agent.createAgent('developer');
const help = await devAgent.chat('How do I deploy a WASM module to my canister?');

Key Features

Context Awareness

Agents maintain conversation context, understanding references to previous operations.

Error Handling

Natural language error messages help users understand and fix issues.

Multi-Step Operations

Complex workflows can be expressed in simple commands.

Tool Transparency

See exactly which tools are being used for each operation.

Available Commands

Here are some example commands you can use:

Identity Management

  • “Show me my current identity”
  • “Create a new identity called alice”
  • “Switch to my trading identity”
  • “Generate a new seed phrase”

Token Operations

  • “Check my ICP balance”
  • “Transfer 10 ICP to bob.icp”
  • “Show me all my token balances”
  • “Transfer 1000 CHAT tokens to principal abc123”

Canister Management

  • “Create a new canister with 5T cycles”
  • “Deploy my WASM to canister xyz”
  • “Check the status of all my canisters”
  • “Add alice as a controller to my canister”

Cycles Management

  • “Check cycles balance for canister abc”
  • “Top up my canister with 2T cycles”
  • “How many cycles can I get with 1 ICP?”
  • “Monitor my canister cycles and alert me when low”

Configuration Options

Model Selection

You can choose different OpenAI models based on your needs:
const agent = new ICPAgent({
  network: 'mainnet',
  openai: {
    apiKey: 'your-api-key',
    model: 'gpt-4-turbo-preview', // Best performance
    // model: 'gpt-3.5-turbo' // Faster and cheaper
  },
});

Environment Variables

The integration supports these environment variables:
  • OPENAI_API_KEY: Your OpenAI API key
  • ICP_NETWORK: Default network (mainnet/local)

Best Practices

What’s Next?