Skip to content

Getting started

Build an autonomous agent that pays for its own inference and onchain compute with Coinbase AgentKit and the @libertai/agentkit-plugin (TypeScript) or libertai-agentkit-plugin (Python).

TIP

Don't want to wire payments yourself? You can also use any LibertAI model with a regular API key from the Developer console and skip the wallet plumbing entirely. See the Quickstart for the simpler path.

Prerequisites

  • A wallet private key with USDC on Base (chain ID 8453)
  • TypeScript: Node.js 18+
  • Python: Python 3.11+

TIP

You need USDC on Base for x402 payments (inference) and optionally ETH for gas if your agent sends onchain transactions.

Installation

sh
npm install @libertai/agentkit-plugin @coinbase/agentkit openai viem

Create a wallet

The plugin provides a helper to create a wallet client wrapped in an AgentKit-compatible provider:

ts
import { createAgentWallet } from "@libertai/agentkit-plugin";
import { base } from "viem/chains";

const privateKey = process.env.WALLET_PRIVATE_KEY as `0x${string}`;
const wallet = await createAgentWallet(privateKey, base);

console.log(`Agent wallet: ${wallet.account.address}`);

Set up AgentKit

Register action providers — these define what tools the agent can use:

ts
import { AgentKit, walletActionProvider, erc20ActionProvider } from "@coinbase/agentkit";
import { createAlephActionProvider } from "@libertai/agentkit-plugin";

const agentkit = await AgentKit.from({
  walletProvider: wallet.provider,
  actionProviders: [
    walletActionProvider(),
    erc20ActionProvider(),
    createAlephActionProvider(privateKey),
  ],
});

AlephActionProvider adds two tools the agent can call:

ToolDescription
get_credits_infoReturns credit balance (USD), cost/day (USD), and runway in days
buy_creditsBuys Aleph credits via x402 payment (amount in USD)

Create the LLM client

The plugin wraps the OpenAI SDK with automatic x402 payment handling:

ts
import { createLLMClient, actionsToTools } from "@libertai/agentkit-plugin";

const openai = createLLMClient(privateKey);
const { tools, executeTool } = actionsToTools(agentkit.getActions());

actionsToTools / actions_to_tools converts AgentKit actions into OpenAI tool definitions and returns an executeTool / execute_tool dispatcher.

Run the agent loop

Put it all together in an autonomous loop:

ts
import type { ChatCompletionMessageParam } from "openai/resources/chat/completions.mjs";

const SYSTEM_PROMPT = `You are an autonomous AI agent on the Base blockchain.
You have a wallet with ETH and USDC. You pay for compute via Aleph credits.

Each cycle:
1. Check your balances (ETH, USDC) and credit info
2. If credits are running low, buy more credits
3. Report your status

Be concise. Only take action when needed.`;

while (true) {
  const messages: ChatCompletionMessageParam[] = [
    { role: "system", content: SYSTEM_PROMPT },
    { role: "user", content: "Check status and take any necessary actions." },
  ];

  let response = await openai.chat.completions.create({
    model: "qwen3.6-35b-a3b",
    messages,
    tools,
  });

  while (response.choices[0]?.finish_reason === "tool_calls") {
    const assistantMessage = response.choices[0].message;
    messages.push(assistantMessage);

    for (const toolCall of assistantMessage.tool_calls ?? []) {
      const result = await executeTool(
        toolCall.function.name,
        toolCall.function.arguments,
      );
      messages.push({
        role: "tool",
        tool_call_id: toolCall.id,
        content: result,
      });
    }

    response = await openai.chat.completions.create({
      model: "qwen3.6-35b-a3b",
      messages,
      tools,
    });
  }

  const text = response.choices[0]?.message?.content;
  if (text) console.log(`Agent: ${text}`);

  await new Promise((r) => setTimeout(r, 60_000));
}

TIP

Pick a model that supports function calling (⚙️ on the models list). Most production agents use qwen3.6-35b-a3b or gemma-4-31b-it.

Adding custom tools

The pattern is the standard Coinbase AgentKit ActionProvider. Subclass and register it like any other action provider — see AgentKit docs for the full API. Once registered, your tool shows up in the tools array returned by actionsToTools / actions_to_tools automatically.

Full examples

Complete working agents in the libertai-agents repository:

See also