Agents
AI agents that coordinate actions to achieve goals
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.
Agent Creation Parameters
Parameter | Type | Required | Description |
---|---|---|---|
instructions | string | Yes | Agent description and task |
model | LanguageModelV1 | Yes | LLM to use for decision making |
actions | Action[] | Yes | Actions the agent can execute |
agentId | string | No | Agent identifier for logging (if using SpinAI’s log tracking) |
spinApiKey | string | No | SpinAI API key for logging (if using SpinAI’s log tracking) |
customLoggingEndpoint | string | No | Custom 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.
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.
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.
Parameter | Type | Required | Description |
---|---|---|---|
input | string | Yes | User input to process |
state | Record<string, any> | No | A state object that can be accessed from inside your actions. Useful for any information your actions may need to run. |
maxSteps | number | No | Maximum number of steps the agent should take |
externalCustomerId | string | No | External customer identifier for logging (if using SpinAI’s log tracking) |
actions | Action[] | No | Actions the agent can execute (overrides the ones passed in to the agent creation) |
model | LanguageModelV1 | No | LLM to use for decision making (overrides the one passed in to the agent creation) |
debug | DebugMode | No | Debug mode for local runtime logging. ‘verbose’ for verbose logging, ‘error’ for error logging, ‘off’ for no logging. |
agentId | string | No | Agent identifier for logging (if using SpinAI’s log tracking) |
spinApiKey | string | No | SpinAI API key for logging (if using SpinAI’s log tracking) |
responseFormat | ZodObject | No | Format of the response (defaults to “text” which returns a string). |
customLoggingEndpoint | string | No | Custom logging endpoint for metrics |
messages | Messages | No | Messages to use for the agent (overrides the ones passed in to the agent creation) |
Agent Response
The agent response object contains the following properties:
Property | Type | Description |
---|---|---|
response | T | The final response of the agent. A string by default, or the zod type you passed into responseFormat |
sessionId | string | The session ID of the agent. |
interactionId | string | The interaction ID of the agent. |
totalDurationMs | number | The total duration of the agent in milliseconds. |
totalCostCents | number | The total cost of the agent in cents. |
totalPromptTokens | number | The total number of prompt tokens processed by the agent. |
totalCompletionTokens | number | The total number of completion tokens processed by the agent. |
state | Record<string, any> | The final state of the agent. |
messages | Messages | The messages exchanged during the agent call. |