Skip to main content

Agent Automations

Run an AI agent by itself: a stored prompt the agent runs on a cron schedule or whenever an event is emitted.​

Why Use Automations​

Your agent answers questions when someone asks. Plenty of work has no one asking: a nightly summary of new support cases, triaging each incoming email, reacting to an SMS. Without automations you would stand up a scheduler or an event handler whose only job is to call the agent.

An automation stores the prompt with the agent and runs it for you:

Backend code
await this.squid.ai().agent('support-copilot').upsertAutomation({
type: 'schedule',
name: 'Nightly case summary',
prompt: 'Summarize the support cases opened in the last 24 hours and file the summary.',
cron: '0 3 * * *',
timeZone: 'America/New_York',
});

Overview​

An automation is a stored prompt an agent runs by itself. There are two kinds:

  • Schedule automation: runs on a cron expression (5 fields, or 6 with seconds), evaluated in an IANA time zone (UTC by default), at most once a minute.
  • Event automation: runs whenever an event of its type is emitted with squid.events().emit(), by your backend code or by a connector. The event is appended to the prompt as data, so the agent sees what triggered it.

Each run is a regular agent turn with the owner's authority and no memory: runs do not see each other or any chat history. The run is tracked as a job, and recorded in the agent's audit log when the agent has auditing on.

Automations are managed with the client SDK on squid.ai().agent(id). Every method requires the app API key, so call them from backend code. An agent can have up to 100 automations. The Console shows them under the agent's Automations tab in Studio, where you can also add, edit, run, and disable them without code.

note

Automation methods are available in the TypeScript client SDK. The Python SDK does not have them yet.

Quick Start​

Prerequisites​

  • An existing agent. The examples use support-copilot; see How to Build an AI Agent to create one.
  • A deployed Squid backend, since automation methods require the app API key.

Step 1: Create a schedule automation​

Backend code
import { executable, SquidService } from '@squidcloud/backend';

export class AutomationSetupService extends SquidService {
@executable()
async createNightlySummary(): Promise<string> {
const automation = await this.squid.ai().agent('support-copilot').upsertAutomation({
type: 'schedule',
name: 'Nightly case summary',
prompt: 'Summarize the support cases opened in the last 24 hours.',
cron: '0 3 * * *', // every day at 03:00
timeZone: 'America/New_York',
});
return automation.id; // generated when you omit `id`
}
}

Step 2: Run it once to verify​

runAutomation() starts a run immediately, whether or not the automation is enabled, and returns the job of the run:

Backend code
const { jobId } = await this.squid.ai().agent('support-copilot').runAutomation(automationId);

// Wait for the run and read the agent's answer.
const result = await this.squid.job().awaitJob(jobId);
console.log((result as { responseString: string }).responseString);

From here, the schedule takes over: the automation's nextRunAt shows when it runs next.

Managing Automations​

All methods live on squid.ai().agent(agentId) and require the app API key:

MethodWhat it does
listAutomations()Lists the agent's automations, oldest first.
getAutomation(automationId)Returns one automation, or undefined when none has that ID.
upsertAutomation(params)Creates an automation, or replaces the one with the same id. An update that leaves out an optional field removes it.
setAutomationEnabled(automationId, enabled)Turns an automation on or off without touching the rest of its definition. Turning a schedule on sets its next run from now.
runAutomation(automationId)Starts a run now, even for a disabled automation, and returns { jobId }.
deleteAutomation(automationId)Deletes an automation. Does not fail if it does not exist.

Every automation also reports its run history fields on read: lastRunAt and lastJobId for the most recent run, nextRunAt for an enabled schedule, and skippedRuns, lastSkippedAt, and lastSkipReason when runs were skipped (see Skipped runs).

Event Automations​

An event automation runs its agent when an event of its type is emitted:

Backend code
await this.squid.ai().agent('support-copilot').upsertAutomation({
type: 'event',
name: 'Triage incoming mail',
eventType: 'mail-received',
prompt: 'Triage this email: label it, and open a case if it reports a problem.',
maxRunsPerHour: 100, // guard against floods; defaults to 1000
});

Anything that calls squid.events().emit() can trigger it, including your own backend code:

Backend code
import { generateUUID } from '@squidcloud/client';

await this.squid.events().emit({
id: generateUUID(), // a re-emit of the same id is dropped while the first run's job record exists
type: 'order-escalated',
payload: { orderId: 'ord_8731', reason: 'late delivery' },
// Optional: override the automation's default prompt for this event only.
prompt: 'An order was escalated. Draft an apology email and propose a credit.',
});

How the prompt is chosen: the event's own prompt wins; otherwise the automation's default prompt is used; an event with neither starts no run. Either way, the event's type, ID, and payload are appended to the prompt as data, marked so the agent treats payload content as information rather than instructions.

Connector events​

Several connectors emit events your automations can listen to:

Event typeEmitted by
mail-receivedThe Mail connector, per received email
slack-webhook-eventThe Slack connector
teams-webhook-eventThe Teams connector
twilio-sms-eventThe Twilio connector, per inbound SMS
ai-voice-session-eventVoice agents, per session milestone (started, ended, state change)

When an agent has several connectors of one type, bind the automation to specific ones with integrationIds; the automation then runs only for events carrying one of those integration IDs:

Backend code
await this.squid
.ai()
.agent('support-copilot')
.upsertAutomation({
type: 'event',
name: 'Support inbox only',
eventType: 'mail-received',
prompt: 'Triage this email.',
integrationIds: ['support-mailbox'], // ignore events from other mail connectors
});

Skipped Runs​

An automation that was due to run but did not records a skip. lastSkipReason tells you why:

ReasonMeaning
no_promptThe event carried no prompt and the automation has no default one.
hourly_limitThe event automation reached its maxRunsPerHour.
overloadedToo many of the application's event runs were already waiting to start.
previous_run_in_progressA schedule came due while its previous run was still going.

Observing Runs​

Automation runs land in the agent's audit log when the agent has auditLog on. Each run entry carries tags: automationId, automationName, automationTrigger (schedule, event, or manual), and for event runs also automationEventType, automationEventId, and automationPromptSource (event or default). Filter the log to one automation's runs:

Backend code
const { entries } = await this.squid.ai().agent('support-copilot').auditLogs({ where: { automationId } });

for (const entry of entries) {
console.log(entry.createdAt, entry.tags['automationTrigger'], entry.prompt);
}

For a run you started with runAutomation(), await its job as shown in the Quick Start. Keep the returned job ID private: anyone holding a job ID can read that job's result.

Error Handling​

ErrorMeaning
API_KEY_REQUIREDThe call was made without the app API key. Call automation methods from backend code.
automation.cron is not a valid cron expression: ...The cron has the wrong shape. Use 5 fields, or 6 with seconds.
automation.cron must not run more often than once a minuteSecond-level crons may not fire more than once a minute.
automation.timeZone is not a valid IANA time zone: ...Use a zone name such as America/New_York, not an offset.
AUTOMATION_NOT_FOUNDsetAutomationEnabled or runAutomation named an automation the agent does not have.
The automation has no default prompt: it runs only for events that carry their ownrunAutomation was called on an event automation without a default prompt.
AUTOMATION_RUN_NOT_STARTEDThe manual run could not start; retry.
An agent may have at most 100 automationsDelete automations you no longer need before adding more.

Deleting an agent deletes its automations. A disabled automation keeps its definition and history but starts no runs.

Best Practices​

  • Give event automations a default prompt unless every emitter sends one. An event with no prompt from either side is silently skipped (as no_prompt).
  • Set maxRunsPerHour to match the expected event rate. The default of 1000 guards against runaway loops, but a triage automation on a busy mailbox deserves a deliberate number.
  • Use the event's id for retry safety, within a window. A re-emit of the same event ID is dropped while the first run's job record exists; finished jobs are swept roughly an hour after they complete, after which the same ID starts a new run. Emit with a stable ID derived from the source record for short-lived retries, and deduplicate in your own data for anything longer.
  • Keep prompts self-contained. Runs have no memory and no chat history; everything the agent needs must be in the prompt, the appended event, or the agent's own instructions and connected sources.
  • Turn on the agent's audit log while developing an automation. The trigger, prompt source, and event tags make it easy to see why a run happened or what it received.