AI
Usage with the Vercel AI SDK
The @thirdweb-dev/ai-sdk-provider
is a lightweight provider that lets you use thirdweb AI with the Vercel AI SDK.
It standardizes message parts, exposes wallet-aware tools (sign_transaction
, sign_swap
), and ships types/utilities so you can build chat UIs that execute on-chain actions from AI responses.
If you're using the Vercel AI SDK (ai
/ @ai-sdk/react
) and want your agent to request blockchain actions safely, this provider gives you:
- A server wrapper that streams AI output and tools.
- A message schema (
ThirdwebAiMessage
) compatible withuseChat
for typed tool results. - Ready-made thirdweb tools wired for transactions, swaps and monitoring.
Server side (Next.js App Router, /app/api/chat/route.ts
)
Create a thirdweb ai provider instance and compatible with the Vercel AI SDK by calling createThirdwebAI
with your project secret key.
Then pass a thirdwebAI.chat()
instance as the model for the streamText
function and configure the model context with the context
option.
For continuous conversations, you can extract the session id from the response and send it back to the client by overriding the messageMetadata
function as shown below.
Client side (React, using useChat
)
Use useChat<ThirdwebAiMessage>()
to get typed objects from useChat()
. This will give you strongly typed tool results in your UI like tool-sign_transaction
, tool-sign_swap
or tool-monitor_transaction
.
You can render messages and reasoning steps as you normally would using your own UI or the Vercel AI Elements components.
When a transaction or swap is requested, the model will return a tool-sign_transaction
or tool-sign_swap
part. You can then render those tool requests in your UI, for example:
tool-sign_transaction
-> render a button to execute the transaction (TransactionButton
works well for this)tool-sign_swap
-> render a button to execute the swaptool-monitor_transaction
-> render a loading indicator with the transaction status (whenauto_execute_transactions
istrue
)
Then on result, you can call the addToolResult
function to add the tool result to the messages array and send it back to the model to continue the conversation.