Background Agents & Scheduling
Relevant source files
The following files were used as context for generating this wiki page:
Background Agents & Scheduling is a core feature of OS Chat that allows users to deploy autonomous AI agents to perform tasks on a one-time or recurring basis (daily or weekly). These agents operate independently of active user sessions, executing instructions, utilizing integrated tools (like Gmail or Notion), and providing results via internal chat logs and optional email notifications.
The system leverages Convex's serverless scheduling capabilities for execution, Redis for persistent agent memory, and the Vercel AI SDK for autonomous tool-calling and reasoning. This architecture enables long-running or periodic workflows such as daily research summaries, automated reporting, or scheduled data synchronization across various third-party services.
System Architecture & Execution Flow
The background agent system is built on a distributed architecture where Convex handles task definitions and scheduling, while Redis maintains state between executions. When a task is triggered, an internal action initializes an autonomous agent context, including specialized system prompts and a toolset.
Task Execution Sequence
The following diagram illustrates the lifecycle of a background agent execution, from the initial trigger to final notification and rescheduling.
Task Configuration & Management
Users configure background agents through a specialized UI that defines the scope, frequency, and capabilities of the agent.
Configuration Parameters
| Parameter | Type | Description |
|---|---|---|
title | string | The display name for the background agent. |
prompt | string | The specific instructions for the agent to execute. |
scheduleType | enum | Frequency: onetime, daily, or weekly. |
scheduledTime | string | Time of day in HH:MM format (or day:HH:MM for weekly). |
timezone | string | The user's local timezone for execution calculation. |
enableSearch | boolean | Whether the agent can use web search tools. |
emailNotifications | boolean | If enabled, the system sends a summary email upon completion. |
Sources: src/components/scheduled-tasks/task-form.tsx:50-70, convex/scheduled_ai.ts:135-150
Frequency & Rescheduling Logic
The system calculates the next execution time based on the scheduleType and timezone provided.
- Daily Tasks: Parsed from
HH:MM, converting user time to UTC and finding the next occurrence. - Weekly Tasks: Parsed from
day:HH:MM(e.g.,1:09:00for Monday at 9 AM). - One-time Tasks: Executed once and then moved to
archivedstatus.
Sources: convex/scheduled_ai.ts:310-360, src/components/scheduled-tasks/task-form.tsx:160-175
Agent Intelligence & Memory
Scheduled agents differ from standard chat agents by their autonomous nature and persistent "Working Memory."
Autonomous Execution (Task Mode)
When running in taskMode, the system prompt is modified to emphasize independence. The agent is instructed to make informed decisions and delivered comprehensive results without requesting user clarification, as there is no user present to respond in real-time.
Sources: src/lib/prompt_config.ts:165-190
Persistent Working Memory
To provide continuity across executions, agents utilize a Redis-backed memory system.
- Load History: Retrieves the last 20 turns of interaction for a specific taskId.
- Working Memory: A structured Markdown block (
# Working Memory) containing key facts, current focus, and user preferences. - Update Tool: Agents can proactively call
updateWorkingMemoryto save new information they learn during a task, ensuring it is available for the next scheduled run.
Notifications & Reporting
Upon successful execution, the system can generate email summaries if configured.
Email Notification System
The execution flow triggers sendTaskSummaryEmail in convex/email.ts. This service:
- Sanitizes Content: Converts agent Markdown output to safe HTML.
- Templates: Uses a monospace terminal-style HTML template to present logs and results.
- Delivery: Sends via Resend, including a direct link to the specific chat session generated for that execution.
Sources: convex/email.ts:80-150, convex/scheduled_ai.ts:285-300
Execution Tracking
Every run is recorded in a history table with the following metadata:
status: success or failure.usage: input, output, and total token counts.duration: server duration in milliseconds.error: specific error message if the task failed.
Sources: convex/scheduled_ai.ts:370-400
Service Integrations
Scheduled agents use the create_agent tool to delegate complex sub-tasks involving external integrations. Toolkits like GMAIL, NOTION, and GOOGLECALENDAR are derived from the user's connected connectors.
Sources: src/lib/create-agent-tool.ts:110-150, convex/scheduled_ai.ts:100-120