AWS Web3 Blog

Build crypto AI agents on Amazon Bedrock

As Web3 and generative AI technologies continue to rapidly evolve, a new category of applications known as crypto AI agents has emerged. These agents use large language models (LLMs) for their intelligence to accomplish a variety of blockchain-related tasks through a supervisor-collaborator architecture. A supervisor agent orchestrates specialized collaborator agents to analyze blockchain data, identify on-chain investment opportunities, and interact with both humans and other agents. These agents can be equipped with cryptocurrency wallets for autonomous transactions when opportunities arise.

Engaging with cryptocurrencies on-chain using AI agents presents several technical challenges:

  • Orchestrating multiple specialized agents for complex tasks
  • Securely maintaining cryptocurrency wallets
  • Integrating real-time and historical data feeds
  • Accessing and analyzing current news
  • Preventing hallucinations through reliable data sources
  • Managing secure blockchain interactions

In this post, we present a solution architecture with an accompanying GitHub repo that deploys the foundational building blocks for a crypto AI agent. The solution leverages several AWS services:

In the first part of this post, we dive into the details of the solution architecture, focusing on how the supervisor agent coordinates with specialized collaborator agents. The second half demonstrates the agent system in action.

Solution overview

The crypto AI agent is designed with the following capabilities:

  • Analyze token industry data, including real-time token prices
  • Estimate gas fees for submitting transactions
  • Query on-chain wallet balances for tokens
  • Provide investment recommendations (these are simulated)
  • Submit transactions to the blockchain (send payment to another address)
  • Aggregate near real-time Web3 news and data
  • Support complex tasks through multi-agent orchestration

The following diagram illustrates the solution architecture and how the various components work together to provide the crypto AI agent with its capabilities.

solution-architecture

Let’s explore each of these components in more detail.

Security Guardrails

Given that our agent has control over financial assets, it’s crucial to implement robust security measures against potential threats, including LLM prompt injection exploits. We use Amazon Bedrock Guardrails to create a protective layer around our agent’s decision-making process. Guardrails help prevent:

  • Unauthorized attempts to modify the agent’s core instructions
  • Social engineering which tries to convince the agent to transfer funds
  • Attempts to bypass transaction limits or security checks
  • Manipulating the agent’s risk assessment capabilities

We implement guardrails through:

  • Input validation rules that detect and block malicious prompts
  • Response filters that make sure that outputs align with predefined security policies
  • Regular validation of the agent’s response patterns

Moreover, we could enforce transaction limits and approval workflows for high-value transfers through the Wallet Management action group’s Lambda function. Additional security controls like this within the business logic are necessary for comprehensive protection of funds.

Guardrails are crucial when deploying AI agents with financial capabilities, as they provide an additional layer of security beyond traditional authentication and authorization mechanisms, which fail to protect against the threat vectors unique to LLMs.

1) Crypto AI Supervisor Agent

The crypto AI agent orchestrates interactions between the user and specialized collaborator agents. Built with Amazon Bedrock Agents, it is a supervisor agent that leverages the Amazon Nova Pro model to coordinate complex tasks and maintain direct user communications. When the supervisor receives a user request, it determines if it needs to engage other collaborator agents, and coordinates their activities with actions it takes itself.

As a fully managed service, Bedrock Agents eliminate the need for infrastructure provisioning and management which enables the agents to scale up in line with user demand.

Each agent has its own set of action groups connected to AWS Lambda functions that invoke specific tasks. Action groups define the required and optional parameters for an action. The Blockchain Data Agent, for example, includes an action group that queries blockchain data from AWS Public Blockchain Data. The supervisor agent includes an action group that takes in a token as a parameter, queries its price, and returns this value.

Each agent needs to be made aware of the action groups it can use. To do this, in the agent’s prompt we include a description of the available tools:

These are the functions you can invoke:
sendtx - send a transaction to the blockchain
estimateGas - estimate the gas cost of a transaction
getBalance - get the balance of a wallet
getCryptoPrice - get the price of a cryptocurrency token
investAdviceMetric - get investment advice
getWalletAddress - get your own wallet’s address
Code

Now when the agent receives a user request and breaks down the request into discrete tasks, it can select an appropriate action group and provide the required or optional parameters. This way, an agent can perform complex tasks by breaking them down into predefined actions with clear input requirements.

2) Knowledge base: Blockchain news crawler

Although the agent’s LLM is well-suited for understanding a user’s request, it doesn’t have the built-in access to external data. To address this problem, Retrieval Augmented Generation (RAG) has emerged as a popular way of providing external data to an LLM and to help prevent hallucinations and improve LLM accuracy. The source of the data can include unstructured data such as the contents of PDFs and Word documents or structured data such as the contents of a relational database, crawled websites, and more. This data must then be chunked and vectorized, making it available in a format that the LLM can query. Amazon Bedrock Knowledge Bases provide a fully managed workflow for end-to-end RAG including ingesting, chunking, vectorizing and storing the data, removing a lot of the heavy lifting that a builder would otherwise have to do.

In our solution, we create a knowledge base that is continuously updated by a web crawler that is crawling blockchain news from news websites to provide current information to our crypto AI agent. The crawler runs every minute, and automatically downloads and vectorizes the data into Amazon OpenSearch Serverless, making it instantly available to the agent when it needs to know about current news.

3) Blockchain Data Agent (collaborator)

We want to equip our crypto AI agent with the ability to query historic blockchain data. This allows it to understand historic trading patterns, such as trading volume for a particular cryptocurrency, to enable it to decide on a trading strategy. For example, understanding the transaction volume over the past month, or the largest transaction that was sent in the past 24 hours. Although we could create an action group for this and attach it to the crypto AI agent, in our solution, we instead create another agent (the Blockchain Data Agent) whose sole functionality is to query historic blockchain data. There are a few advantages to using specialized agents in place of enabling this specialized functionality on the Supervisor via its own Action Groups:

  • The specialized agent can be reused by other agents
  • The specialized agent can have a deeper, specific understanding of a task or industry
  • The specialized agent can have enhanced accuracy in responses within its designated field

The Blockchain Data Agent sources its data from AWS Public Blockchain Data. For more information on how the Blockchain Data Agent is built, refer to the GitHub repo and associated blog post, Analyze blockchain data with natural language using Amazon Bedrock.

4) Wallet Management

We want our agent to be able to operate autonomously, which includes sending transactions from its own cryptocurrency wallet when it needs to complete a task, such as sending payment or invoking a smart contract on-chain. The Wallet Management action group is used by the crypto AI agent to query wallet balances and send transactions to the blockchain network. To enable Wallet Management to fulfill these transactions, we provide a blockchain Remote Procedure Call (RPC) endpoint to its Lambda function, which it uses to query on-chain data and send transactions. In the solution we use Amazon Managed Blockchain, but this can be replaced with another RPC endpoint.

Cryptocurrency wallets are based on asymmetric key encryption, which involves a public key and a private key. The private key must be carefully secured to make sure it’s not lost or leaked, protecting against loss of funds. In our solution, we use the Polygon blockchain network which is an Ethereum Virtual Machine (EVM)-based blockchain. To create, secure, and use our wallet, we use AWS KMS to create an asymmetric KMS key. AWS KMS natively supports the cryptography that is used within the EVM and its wallets. Refer to this blog post for a deeper understanding of how KMS can be used for EVM wallets.

The wallet’s private key cannot be exported from AWS KMS, helping to protect the agent from having its key leaked. We further safeguard this key by restricting access to it to only the Lambda function contained in the agent’s Wallet Management action group.

Because we want to enable our agent to operate autonomously, sending transactions and payments when it deems it necessary, we want to enforce guardrails to protect the user and make sure that the agent is operating within acceptable boundaries. To help prevent overspending, we can enforce spending limits within the action group, such as per transaction limits, or maximum spend within a 24-hour period. To make sure the agent is operating within regulatory compliance we can create an allowlist of permitted blockchain addresses it can interact with, or conversely, create a blocklist of any addresses it must not interact with. We also want to keep separate the wallets the agent is operating from personal wallets a user may have, to make sure the agent cannot accidentally spend a user’s assets. There are many other constraints to be considered and these are just a few examples.

Crypto AI agent demo

Now that we’ve seen how we the crypto AI agent is built, let’s see it in action.

Get real-time token price

The agent can process queries about token prices using both full names (such as Bitcoin), and token symbols (such as BTC). The agent translates the user input into a standardized token name, invokes the action group with this token name, and retrieves the price from the external pricing provider.

Here we ask the agent the price of Bitcoin, Ethereum, and DOGE, phrasing our question differently each time.

ask-token-pricing

Retrieve gas cost of sending a transaction

We can ask the agent to give us an estimate on gas cost for a transaction.

cost-of-payment

Retrieve historic blockchain data

We can ask the agent how many bitcoin transactions occurred yesterday.

how-many-btc-txs

We can ask the agent questions related to transaction size, token transfer volume, and smart contract data.

ask-questions-historic-data

Retrieve agent’s wallet address

To conduct transactions on on-chain actions, a crypto AI agent needs assets in their self-managed wallet. To send assets to this wallet, we first need to know the agent’s wallet address, which we can ask it for. The crypto AI agent can retrieve its wallet address via its Wallet Management action group which derives the address from its KMS key’s public key.

We ask the agent for its wallet address.

askwalletaddres

Query wallet balances

We can ask the agent to query on-chain balances of various tokens and for specific wallets. This uses the Wallet Management action group and supports ENS domain resolution.

querywalletbalance

Transfer payment

We can use the agent to transfer funds to another address. The agent can be given the destination wallet address or an ENS name, in which case it looks up the associated wallet address. The agent then constructs the transaction, signs it with KMS, and then sends it to the network via the blockchain RPC endpoint. It returns the blockchain transaction hash which we can use to track the transaction’s status.

Since the agent is transferring funds, we can enable additional user protection by enabling the action group’s RequireConfirmation flag. When enabled, the agent will prompt the user for a final confirmation of this action before it completes it.

transfer-payment

After confirming the action, the agent submits the transaction and returns the transaction hash. The hash can be used to look up the transaction on a blockchain explorer.

polygon-scan

News and Market Intelligence

The crypto AI agent leverages the blockchain news knowledge base to provide current industry insights and trends, offering context for user queries about recent developments in the Web3 space.

We ask the agent about blockchain related news, which the agent queries the Knowledge Base to retrieve its answer.

knowledge-base-news-query

Conclusion

In this post, we outlined a solution architecture for building a crypto AI agent on Amazon Bedrock and other AWS services. The solution explored using the agent to help with understanding industry price activity and trends, making informed trading decisions, and making payments on the network. However, the design space for crypto AI agents is much broader, and there have already been experiments with agents autonomously deploying their own contracts, launching their own meme coins, and engaging with other agents.

The solution is publicly available on GitHub and is designed to be extendable to add more capabilities. For example, to support blockchains like Solana, whose wallets are not natively supported by AWS KMS, you can implement key management as shown in this GitHub repository. Let us know if you decide to try building your own crypto AI agent. We can’t wait to see what you build.


About the Authors

emile-baizelEmile Baizel is a Senior Blockchain Architect at AWS. He began working with blockchain in 2018 when he participated in his first Ethereum hackathon. He is currently focused on helping builders that are working at the intersection of blockchain and generative AI.

zhiyu-haohaozhiyu is a Solutions Architect at AWS and a blockchain enthusiast. He works with customers to build innovative solutions, helping them solve business challenges and rapidly adopt AWS services. Outside of work, he is passionate about various extreme sports, including paragliding, ultimate frisbee, and freediving.