On-Chain AI Agents: Exploring Their Potential in Web3
How AI Agents Work, Their Applications in Web3, and a Hands-on Demo to Fetch Wallet Balances
It has been an innovative year since the beginning of 2025, particularly in many domains, but AI has been progressing rapidly with numerous new models demonstrating significant improvements.
One topic that has garnered a lot of attention recently is AI Agents, especially on-chain AI agents. Curious about their potential, I decided to explore AI Agents to understand what they can actually do and how we can utilize them in the web3 space.
This article highlights what I learned about AI agents, how they work, and an application I experimented with to gain further insight.
How it works
AI agents are autonomous systems that observe their surroundings, make decisions, and perform actions to achieve specific tasks.
During my learning journey, I used OpenAI Assistant APIs to experiment with creating an AI Agent. With the Assistant, we can provide both instructions and tools to develop our own AI Agent. The instructions guide the Assistant on what actions to take and how to behave, while the tools are a crucial aspect of this process. We can pass custom functions on to our assistant as tools.
Based on the instructions and the user's prompt, the Assistant can determine which function to call from the list of functions we've included as tools. Pretty awesome, huh?
Let's take a look at an example. Suppose we have an AI agent designed to provide us with information about the weather. Behind the scenes, this AI assistant is programmed with instructions that state it knows everything about weather-related topics. It is equipped with various functions or tools related to weather details, such as retrieving the current temperature or checking if it is raining on a given day.
When we ask a question about the weather, like "Is it raining today?", the agent will take the input, analyze it, and determine which tool to use to obtain the answer. In this case, it will decide to use a tool called "is_raining" to check the rain status and will trigger the function associated with it. This function could be written in any programming language and must call a weather API to fetch the relevant information, which it will then send back to the agent.
Once the agent receives the result from the tool it invoked, it will wrap the information in a nice response and send it back to the user.
How Web3 Can Utilize AI Agents
Now the question is how Web3 can utilize AI agents and their capabilities.
Let's say you want to know how a specific token is performing in the market. You would probably visit a site like CoinMarketCap to check the token's price and see how it has fluctuated over time to gather some insights. But what if I told you that instead of doing all that work ourselves, we could build an AI agent to handle it for us and provide the final output as an answer?
That's right! Web3 can leverage AI agents to develop numerous applications. Remember the tools I mentioned earlier? We can call smart contract functions within those tools to trigger on-chain actions based on users' prompts.
As an example, let's instruct our assistant to act as a tokenomics analyst with comprehensive knowledge about various tokens. Next, we will attach a set of tools that can perform tasks such as fetching token prices and checking token balances.
When we ask about the price of a specific token, the AI agent will determine which tool to call in order to provide us with the response.
A Demo Application to Understand it better
You can access the demo application from here.
Let me walk you through the features we have in the code. This agent is going to help us to fetch a balance of a wallet address we provided.
Create an Assistant
The first step is to create an assistant. To do this, we can use the OpenAI SDK to access all its functionalities, including setting up the assistant.
As you can see, we are providing instructions to customize its personality, and capabilities along with tools and a desired model for the assistant during the creation process.
Create a Thread
The next step is to create a message thread. The purpose of creating a thread is to persist the message history of the Agent. We can use the same thread to ask questions about a specific topic and receive all our answers as messages there from the Agent.
Create a Run
This is the most important part of the AI Agent. Assistant can utilize its configuration and the messages in the thread to determine which tool to call during its execution. There may be tool calls, or there might not be any relevant tool calls, allowing the agent to provide an appropriate response.
Reply messages will be sent to the thread as part of the run process.
Each run will have its own lifecycle, starting from a queued state. Depending on the status of the run, specific actions can be performed. In our case, we will continuously poll the run if it is in either the in-progress or queued state and update the run we created to reflect its latest status.
Trigger A Tool Call
Once the assistant realizes that it needs to trigger a tool, it will change the run status to the "requires action" stage. From that point onward, it will select the appropriate tool and initiate the function to obtain the answer.
Each run status may have several error stages, including "expired," "cancelled," and "failed." We can handle these conditions separately to send different error messages back to the user.
When the run status is in the required actions stage, we can call a function that will help the assistant determine which tool function to call and submit the output back to the created run.
Putting it all together
The final step is to integrate all the functions and execute each one to achieve the desired output. As outlined below, we can begin by creating an assistant and then setting up a thread with a message. After that, we can call the run functions to create a Run and trigger tool calls.
When we run the application with the provided message, we can see that it will return the result below. The Assistant will check the balance of the specified wallet address and display it as shown below.
As we discussed throughout our article, AI agents are capable of many things, and it's essentially going to change the way we think about building applications and how we interact with them.
To learn more about AI Agents and how to use OpenAI APIs to build them, I highly recommend checking out their documentation, as it explains all the steps we covered in more detail.