message-botHow to Build an AI Agent On Solana

Learn how to build an AI agent that swap token, checks balance and transfers on Solana

AI agents are autonomous systems that perform operations or tasks without human intervention. These agents act independently, as set up by users. For example, if you set up your agent to send a reminder or a report to your friend, this agent will perform the task on your behalf without your friend knowing that the action was performed by itself.

In the blockchain context, AI agents can:

  • Execute token swaps when prices hit targets

  • Rebalance portfolios across DeFi protocols

  • Monitor wallets and automate transfers

  • Mint NFTs based on social media triggers

Core Stack of AI Agents

Just like every area of development has its own stack for building applications, AI agent development has its own stack, typically consisting of three layers:

  1. Large Language Models (LLMs): The reasoning brain of every AI agent. LMs handle natural language understanding, interpret user requests, process blockchain data, and decide which actions to take.

  2. Agent Frameworks: These define the agent's structure, personality, and interactions with external platforms. Examples include ElizaOS, LangChain, Rig, and AgentiPy.

  3. Blockchain Toolkits: SDKs that enable agents to interact with networks like Solana. Examples include the Solana Agent Kit and GOAT Toolkit.

Why Are AI Agents Important in Blockchain?

  1. Simplification of Complex Tasks: AI agents can handle complex DeFi tasks, such as finding the best yield, rebalancing portfolios, or executing arbitrage trades faster than humans.

  2. Enhanced Security: They provide continuous monitoring of vulnerabilities in smart contracts and wallets and detect fraudulent activity in real time.

  3. Simplified User Experience (Web3 Adoption): AI agents act as intermediaries, translating natural-language requests into complex on-chain actions, reducing technical barriers for users.

  4. Decentralized Autonomous Interactions: Agents can hold funds, sign transactions, and interact with dApps autonomously, creating new, self-sustaining, AI-driven economic ecosystems.

  5. Intelligent Data Processing: They provide an insightful report on vast, immutable datasets on the blockchain to support better decision-making.

Now that you've learnt about AI agents, their core stack, and its importance. In this guide, you will be learning how to build an AI agent on Solana, a fast and growing blockchain network. This agent will be able to transfer SOL to USDC and check your account balance as seen below:

Prerequisites

You must have the following:

  1. Node.js v23+arrow-up-right is installed on your laptop

  2. API keys such as OpenAIarrow-up-right (for the LLM) and Solana Devnet RPC URL (Optional: GetBlock Solana RPC URLarrow-up-right)

  3. Package manager installed on your laptop(Preferrably pnpmarrow-up-right)

circle-info

Devnet RPC URL is used for this guide. If you want to interact with Mainnet, then make use of GetBlock Solana RPC URLarrow-up-right

Project Setup

1

Create a new project directory

2

Install the dependencies:

chevron-rightPackages Descriptionhashtag

Package

What it does

@goat-sdk/core

The brain of GOAT — connects your wallet to AI-usable tools

@goat-sdk/wallet-solana

Lets GOAT read and control your Solana wallet

@goat-sdk/plugin-jupiter

Allows the agent to swap tokens using Jupiter DEX aggregator

@goat-sdk/plugin-spl-token

Lets the agent send, receive, and check balances of SPL tokens (USDC, BONK)

@goat-sdk/plugin-pumpfun

Lets the agent buy and sell tokens on Pump.fun

@goat-sdk/adapter-langchain

Converts GOAT tools into a format LangChain agents can call

@langchain/core

Core building blocks for the LangChain AI framework

@langchain/langgraph

Runs the agent loop — handles thinking, tool calling, and replying

@langchain/openai

Connects LangChain to OpenAI so GPT-4o can power the reasoning

@solana/web3.js

Standard library for talking to the Solana blockchain and RPCs

bs58

Decodes base58 private keys into a format the code can use

dotenv

Loads your .env file so secrets are not hardcoded in the source

3

Create a .env file and add the following details:

4

Create agent.ts file, this is where you will be building your AI agent

5

Import your dependencies:

6

Initialize your Agent

This:

  • Connect the AI agent to Solana Devnet and access your wallet to perform actions

  • Empower the agent with tools for swapping(jupiter()), launch token(pumpfun()) and send and receive SPL token(splToken())

  • Specify the language model to use

  • Create short-term memory so it remembers previous messages within the same chat session.

7

Create Interface for interaction:

This defines an interactive CLI chat loop between you and the AI agent and also logs any errors.

8

Run the agent using this command

You can make any request in any language, as seen below:

You can see that it interprets your natural language, uses the tools plugin to get the right response for it

Extend AI Agent capacity

Improve the capacity of your agent by adding more tools:

  1. Mint NFT:

Best Practice

  1. Store private keys in .env only — never commit this file.

  2. Use a dedicated burner wallet with minimal funds for testing

  3. On mainnet, double-check all transactions before confirming.

Conclusion

In this guide, you have learn what AI agents are, their core stack, and how to build one for yourself to send, swap, launch a token, and check your account balance.

Additional Resources

Last updated

Was this helpful?