Trigger.dev for AI Workloads
Why Background Jobs for AI?
Serverless functions have 10-60s time limits. AI workloads often need:
- Agent loops that run for minutes (research, code generation)
- Batch processing of 1000s of documents through LLMs
- Multi-step pipelines with retries on each step
- Scheduled tasks (daily report generation, model retraining)
AI Agent Workflow Example
import { task } from '@trigger.dev/sdk/v3';
export const codingAgent = task({
id: 'coding-agent',
maxDuration: 600, // 10 minutes
run: async ({ issue }: { issue: string }) => {
// Multi-step agent loop
let plan = await callLLM(`Plan how to fix: ${issue}`);
let code = await callLLM(`Write code for: ${plan}`);
let tests = await runTests(code);
// Retry loop if tests fail
for (let i = 0; i < 3 && !tests.passed; i++) {
code = await callLLM(`Fix failing tests: ${tests.errors}`);
tests = await runTests(code);
}
return { code, testsPassed: tests.passed };
},
});Batch Processing
export const batchEmbed = task({
id: 'batch-embed',
run: async ({ documents }: { documents: string[] }) => {
const results = await Promise.all(
documents.map(doc =>
callOpenAI(`Generate embedding for: ${doc.slice(0, 1000)}`)
)
);
await saveEmbeddings(results);
return { processed: documents.length };
},
});
// Trigger with 10,000 documents
await batchEmbed.trigger({ documents: allDocs });Scheduled Jobs
import { schedules } from '@trigger.dev/sdk/v3';
export const dailyReport = schedules.task({
id: 'daily-ai-report',
cron: '0 9 * * *', // Every day at 9 AM
run: async () => {
const metrics = await getYesterdayMetrics();
const report = await callLLM(`Generate daily AI usage report: ${JSON.stringify(metrics)}`);
await sendEmail({ to: 'team@company.com', body: report });
},
});Built-in Observability
Every task execution is traced:
- Step-by-step execution timeline
- Input/output for each step
- Error details with stack traces
- Duration and retry history
- Cost tracking (if configured)
FAQ
Q: What is Trigger.dev? A: Trigger.dev is an open-source background job framework with 14,300+ GitHub stars for running long-duration AI tasks, multi-step workflows, batch processing, and scheduled jobs with built-in retries and observability.
Q: How is Trigger.dev different from n8n or Temporal? A: Trigger.dev is code-first TypeScript (not visual/YAML). n8n is a visual workflow builder. Temporal is a distributed workflow engine for large teams. Trigger.dev is the simplest path from "I have a TypeScript function" to "it runs reliably in production."
Q: Is Trigger.dev free? A: Yes, open-source under Apache-2.0. Self-host for free. Managed cloud has a free tier.