Agents are the core building blocks of SpinAI. They combine your chosen LLM with actions to solve tasks the way a human decision maker would.

They control what actions to call, what parameters to pass into those actions, all while maintaining state between them and keeping track of the execution history.

Creating an agent

To create an agent, use the createAgent function with any of the models supported by Vercel’s AI SDK. Note the model will automatically read your local .env file to authenticate itself.

import { createAgent } from "spinai";
import { openai } from "@ai-sdk/openai";

const calculatorAgent = createAgent({
  instructions: `You are a calculator agent.`,
  actions: [sum, minus],
  model: openai("gpt-4o"),
});

Agent Creation Parameters

ParameterTypeRequiredDescription
instructionsstringYesAgent description and task
modelLanguageModelV1YesLLM to use for decision making
actionsAction[]YesActions the agent can execute
agentIdstringNoAgent identifier for logging (if using SpinAI’s log tracking)
spinApiKeystringNoSpinAI API key for logging (if using SpinAI’s log tracking)
customLoggingEndpointstringNoCustom logging endpoint for metrics

Calling your agent

To call an agent, it’s as simple as using it as a function while passing an object in.

const { response } = await calculatorAgent({
  input: "What is 5 plus 3 - 1?",
});

Calling your agent with a custom response format

You can also call your agent with a custom response format by passing in a Zod object.

import { z } from "zod";

const responseSchema = z.object({
  finalNumber: z.number(),
});

const { response } = await calculatorAgent({
  input: "What is 5 plus 3 - 1?",
  responseFormat: responseSchema,
});

console.log(response.finalNumber);

Agent Run Parameters

Note: Some parameters are the same as the agent creation parameters, but they can also be passed when calling the agent. Any duplicate parameters passed in to the agent run call will override the ones passed in to the agent creation. This allows you to be flexible with things like using different LLM models, and actions, for an existing agent at different times.

ParameterTypeRequiredDescription
inputstringYesUser input to process
stateRecord<string, any>NoA state object that can be accessed from inside your actions. Useful for any information your actions may need to run.
maxStepsnumberNoMaximum number of steps the agent should take
externalCustomerIdstringNoExternal customer identifier for logging (if using SpinAI’s log tracking)
actionsAction[]NoActions the agent can execute (overrides the ones passed in to the agent creation)
modelLanguageModelV1NoLLM to use for decision making (overrides the one passed in to the agent creation)
debugDebugModeNoDebug mode for local runtime logging. ‘verbose’ for verbose logging, ‘error’ for error logging, ‘off’ for no logging.
agentIdstringNoAgent identifier for logging (if using SpinAI’s log tracking)
spinApiKeystringNoSpinAI API key for logging (if using SpinAI’s log tracking)
responseFormatZodObjectNoFormat of the response (defaults to “text” which returns a string).
customLoggingEndpointstringNoCustom logging endpoint for metrics
messagesMessagesNoMessages to use for the agent (overrides the ones passed in to the agent creation)

Agent Response

The agent response object contains the following properties:

PropertyTypeDescription
responseTThe final response of the agent. A string by default, or the zod type you passed into responseFormat
sessionIdstringThe session ID of the agent.
interactionIdstringThe interaction ID of the agent.
totalDurationMsnumberThe total duration of the agent in milliseconds.
totalCostCentsnumberThe total cost of the agent in cents.
totalPromptTokensnumberThe total number of prompt tokens processed by the agent.
totalCompletionTokensnumberThe total number of completion tokens processed by the agent.
stateRecord<string, any>The final state of the agent.
messagesMessagesThe messages exchanged during the agent call.

Next Steps