Learn how to create and deploy AI agents using the Truffle AI SDK.

This guide will help you create your first AI-powered application.

Initialize the SDK

First, install the Truffle AI SDK:

npm i truffle-ai@latest

Then, import and initialize the Truffle AI SDK with your API key:

import { TruffleAI } from 'truffle-ai';

// Put your API key here
const truffle = new TruffleAI('your-api-key');

Create Your First Agent

Let’s create a simple AI assistant that can help with various tasks:

// Define your agent's configuration
const agentConfig = {
  name: 'My Assistant',
  instruction: 'You are a helpful AI assistant that provides clear and concise responses.',
  model: 'gpt-4o-mini'
};

// Deploy the agent
const agent = await truffle.deployAgent(agentConfig);
console.log(`Agent deployed with ID: ${agent.getId()}`);

How to use your agent

Once your agent is deployed, you can start using it to run tasks:

// Run a simple task
const result = await agent.run('Write a haiku about coding.');
console.log(result);

Working with Existing Agents

You can easily work with previously deployed agents:

// Load an existing agent
const existingAgent = await truffle.loadAgent('agent-id');

// Use the loaded agent
const result = await existingAgent.run('Hello!');

Error Handling

Implement proper error handling in your applications:

try {
  const agent = await truffle.deployAgent(agentConfig);
  const result = await agent.run('Generate a response');
} catch (error) {
  if (error.status === 401) {
    console.error('Authentication failed. Check your API key.');
  } else if (error.status === 400) {
    console.error('Invalid request:', error.details);
  } else {
    console.error('An error occurred:', error.message);
  }
}

Next Steps

Now that you’ve created your first agent, explore more advanced features: