---
title: "Scheduled Tasks"
description: "Scheduled tasks let agents run recurring work such as dependency updates, report generation, and health checks without a human prompt."
sidebar:
  label: "Scheduled tasks"
seo:
  title: "Scheduled Tasks | Autohand Code Agent SDK"
  description: "Run Autohand Code Agent SDK agents on cron schedules. Use durable alarms, scheduled Workers, and long-running processes to automate recurring work."
---

## Scheduling options

- **Cloudflare Workers cron triggers** - run a lightweight scheduler that starts an agent session.
- **Durable Object alarms** - persist the next run time and resume reliably after restarts.
- **System cron** - run a local script with cron on a server or container.
- **Workflow schedulers** - use Kubernetes CronJobs, AWS EventBridge, or GitHub Actions scheduled workflows.

## Cron trigger example

Run recurring checks from the scheduler your stack already uses, then keep the prompt narrow enough for predictable output.

```js title="JavaScript"
export default {
  async scheduled(controller, env, ctx) {
    ctx.waitUntil(env.AUTOHAND.run({
      cwd: '/app',
      prompt: 'Find stale issues and draft follow-up comments.'
    }));
  }
};
```

```ts title="TypeScript"
import { AutohandSDK } from '@autohandai/agent-sdk';

export default {
  async scheduled(controller: ScheduledController, env: Env, ctx: ExecutionContext) {
    const sdk = new AutohandSDK({ cwd: '/app', apiKey: env.AUTOHAND_KEY });
    ctx.waitUntil(sdk.run('Find stale issues and draft follow-up comments.'));
  }
};
```

```python title="Python"
import asyncio
from autohand_sdk import AutohandSDK

async def hourly():
    async with AutohandSDK(cwd="/app") as sdk:
        await sdk.run("Find stale issues and draft follow-up comments.")

if __name__ == "__main__":
    asyncio.run(hourly())
```

```go title="Go"
func runHourly(ctx context.Context) error {
  sdk := autohand.NewClient(autohand.Config{Cwd: "/app"})
  _, err := sdk.Run(ctx, "Find stale issues and draft follow-up comments.")
  return err
}
```

```java title="Java"
@Scheduled(cron = "0 0 * * * *")
void runHourlyReview() {
  var sdk = AutohandClient.builder().cwd("/app").build();
  sdk.run("Find stale issues and draft follow-up comments.");
}
```

```swift title="Swift"
let timer = AsyncTimerSequence(interval: .seconds(3600))

for await _ in timer {
    let sdk = AutohandClient(cwd: "/app")
    _ = try await sdk.run("Find stale issues and draft follow-up comments.")
}
```

## Durable scheduling

For tasks that must not run twice or must resume after a crash, store state in a Durable Object. Set an alarm for the next execution time and confirm completion before scheduling the next run.

## Best practices

- Use short timeouts for scheduled agents to avoid runaway costs.
- Write output to durable storage, not stdout, so you can inspect results later.
- Track the last run timestamp and avoid overlapping executions.
- Alert on repeated failures with a separate hook.
