Skip to content

Get Balance Action

This tool allows the agent to check its own balance, either for the native chain token (ETH/MATIC) or for a specific ERC-20 token.

The LLM invokes this tool when the user asks questions like:

  • “How much ETH do you have?”
  • “What’s your USDC balance?”
  • “Do you have enough funds to send 5 MATIC to vitalik.eth?”
ArgumentTypeDescription
tokenAddressstringOptional. The contract address of the ERC-20 token to check. If omitted or set to null, the action returns the native token balance.
import { AgentKit, GetBalanceAction } from "@0xgasless/agent-sdk";
const agentKit = new AgentKit({ apiKey: "YOUR_API_KEY" });
const action = new GetBalanceAction();
// Check native balance
const ethBalance = await action.invoke(agentKit, {});
console.log(ethBalance); // "0.5 Native Tokens"
// Check USDC balance
const usdcBalance = await action.invoke(agentKit, {
tokenAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
});
console.log(usdcBalance); // "100.5 USDC"

The action reads the agent’s Smart Account address from the AgentKit context.

  • For native balances, it calls client.getNativeBalance().
  • For ERC-20 balances, it calls client.getTokenBalance(tokenAddress), which automatically fetches the token’s decimals and symbol to format the output nicely for the LLM.

The LLM determines when to use GetBalanceAction. To improve reliability, the SmartTransferAction prompt explicitly tells the LLM: “If you are asked to transfer tokens, you should use GetBalanceAction first to check if you have enough balance.” This multi-step reasoning is handled automatically by LangChain’s ReAct agent loop.