> ## Documentation Index
> Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Agents with Triggers

> Run agents on a cron schedule or on demand, inspect run history, and manage trigger lifecycles.

Triggers run agents automatically on a schedule—ideal for daily digests, scheduled inspections, data synchronization, periodic reports, and automated maintenance. In the default `new_session` mode, each run creates a new session; in `reuse_session` mode, the trigger reuses a platform-managed session that is automatically created and bound to the trigger on the first run, so you do not need to specify a session ID. You can also run a trigger manually at any time.

Platform-managed triggers (such as Dreams policies) cannot be modified through the endpoints on this page; see [Dreams](https://platform.kimi.ai/docs/hosted-agents/dreams.md) for how to manage them. This page covers triggers that you create yourself.

The examples below show both cURL and the kha CLI. You can create, view, and manage triggers in **[Console > Triggers](https://platform.kimi.ai/docs/hosted-agents/console.md#triggers)**, or run the same operations with the **[kha CLI](https://platform.kimi.ai/docs/hosted-agents/kha.md)**.

## Create a scheduled trigger

Call `POST /v1/triggers` to create a trigger. For example, to generate a financial market morning brief every weekday at 9:00 AM:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/triggers" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "name": "Weekday Morning Brief",
    "agent_id": "agent_your_agent_id",
    "environment_id": "env_your_environment_id",
    "session_mode": "reuse_session",
    "policy": {
      "type": "cron",
      "expression": "0 9 * * 1-5",
      "timezone": "Asia/Shanghai"
    }
  }'
```

```bash CLI theme={null}
kha triggers create --name "Weekday Morning Brief" \
  --agent agent_your_agent_id \
  --environment env_your_environment_id \
  --session-mode reuse_session \
  --cron "0 9 * * 1-5" \
  --timezone Asia/Shanghai
```

The required fields are `name`, `agent_id`, `environment_id`, and `policy`. `policy.expression` is a space-separated cron expression with five fields—minute, hour, day of month, month, and day of week—and a minimum granularity of 1 minute (`0 9 * * 1-5` above means 9:00 AM on weekdays). Expressions with no matching fire time within the next 5 years are rejected on creation or update. `timezone` is the IANA time zone used to evaluate scheduled run times.

`agent_id` comes from `id` in the create-agent response. `environment_id` comes from `id` in the create-environment response and specifies the execution environment for triggered sessions; omitting it or passing an empty value is rejected (`400` `invalid_parameter_error`). When `agent_version` is omitted, each run uses the agent's latest version at fire time. On creation, the platform validates references to the agent, execution environment, vaults, and memory stores; nonexistent, cross-project, or archived references are rejected.

Creation returns `201` on success; save `id` from the response as `TRIGGER_ID` (`trig_` prefix). Newly created triggers start in `active` status and fire automatically on schedule.

Optional fields:

* `session_mode`: `new_session` (default) or `reuse_session`; the creation example above explicitly sets `reuse_session`.
* `initial_events`: Initial events delivered to the session on each run (such as task instructions), at most 16 entries; currently only `user.message` is supported.
* `resources`: Files, vaults, memory stores, or GitHub repositories mounted into the triggered session, at most 16 entries. Repositories use the `github_repository` structure; see [GitHub Repositories](https://platform.kimi.ai/docs/hosted-agents/github.md).
* `notification`: Webhook configuration for run result notifications; see "Receive run notifications" below.

Each project can have at most 200 non-archived triggers.

## View triggers and scheduled run times

Call `GET /v1/triggers` to list triggers, ordered newest first. Archived triggers are excluded by default; filter by lifecycle status with `status` or by policy type with `policy_type`. The endpoint returns 50 items per page by default; if the response includes `next_page_token`, pass it as `page_token` on the next request until `next_page_token` is empty.

Call `GET /v1/triggers/{id}` to inspect a single trigger:

```bash cURL theme={null}
curl "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha triggers get "$TRIGGER_ID"
```

When a trigger is in `active` status with a cron policy, the response includes `upcoming_runs_at`: up to the next 5 scheduled fire times in UTC, which you can use to verify that the schedule matches your expectations.

## Run manually

For debugging or ad-hoc runs, call `POST /v1/triggers/{id}/run` to trigger an immediate run without waiting for the next scheduled time. Paused triggers can also be run manually:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID/run" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "payload": "Generate this edition of the morning brief now"
  }'
```

```bash CLI theme={null}
kha triggers run "$TRIGGER_ID" --payload "Generate this edition of the morning brief now" --wait
```

`payload` is optional text: when omitted, the trigger runs using only its existing configuration; when provided, it is delivered as a user message to the session for this run.

Manual runs are synchronous: the endpoint waits for the trigger run to finish and responds with `201` along with the terminal run record (`completed`, `failed`, or `skipped`). If another manual run is already in progress for the same trigger, the request returns `400` `failed_precondition_error`. In `reuse_session` mode, if a scheduled run or another run is currently using the managed session, the run is recorded as `skipped` with `error.type` set to `overlap_in_flight`.

Note what `completed` means: it indicates only that the session for this run was created and its initial events were accepted—not that the agent has finished the task. To check task execution and results, use `session_id` from the run record to query session status or subscribe to the event stream; see [Create a Session](https://platform.kimi.ai/docs/hosted-agents/sessions.md) and [Event Stream](https://platform.kimi.ai/docs/hosted-agents/event-stream.md).

## View run history

Call `GET /v1/triggers/{id}/runs` to list run records, ordered newest first; pass `has_error=true` to list only failed runs. The endpoint returns 50 items per page by default; if the response includes `next_page_token`, pass it as `page_token` on the next request to fetch the next page:

```bash cURL theme={null}
curl "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID/runs" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha triggers runs "$TRIGGER_ID"
```

Each run record's `status` field can have the following values:

* `running`: The run record has been created and the session is starting.
* `completed`: The session was created and initial events were accepted (does not indicate that the agent task itself succeeded).
* `failed`: The trigger run failed; see `error` on the record for the failure details.
* `skipped`: The trigger was not active at fire time, or the previous run in `reuse_session` mode had not yet finished.

`context` distinguishes how the run was initiated: `schedule` indicates a scheduled run (including `scheduled_at`), and `manual` indicates a manual run (including `actor`). `session_id` identifies the session used for the run, which you can query to track task progress.

Each trigger retains up to 500 of its most recent run records, and records older than 90 days are deleted.

## Pause and resume

To temporarily disable scheduled runs, call `POST /v1/triggers/{id}/pause` to pause the trigger:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID/pause" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha triggers pause "$TRIGGER_ID"
```

Pausing returns `204` and stops scheduled runs. Any session already running is unaffected, and manual runs remain available. Pausing an already paused trigger is a no-op. If an unrecoverable run error occurs, the platform also pauses the trigger automatically; inspecting the trigger in that state shows `paused_reason.type` set to `error` along with error details.

Call `POST /v1/triggers/{id}/unpause` to resume the trigger:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID/unpause" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha triggers unpause "$TRIGGER_ID"
```

Resuming returns `204` and recalculates the schedule from the current time forward; scheduled fire times missed while paused are not backfilled.

## Update trigger configuration

Call `PATCH /v1/triggers/{id}` to update a trigger. This endpoint performs a partial update: only fields present in the request body are modified, while omitted fields remain unchanged. The example below moves the schedule to 8:30 AM on weekdays and clears bound resources (files, vaults, memory stores, or GitHub repositories) and initial events:

```bash cURL theme={null}
curl --request PATCH "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "resources": [],
    "initial_events": [],
    "policy": {
      "type": "cron",
      "expression": "30 8 * * 1-5",
      "timezone": "Asia/Shanghai"
    }
  }'
```

```bash CLI theme={null}
kha triggers update "$TRIGGER_ID" --file - <<'JSON'
{
  "resources": [],
  "initial_events": [],
  "policy": {
    "type": "cron",
    "expression": "30 8 * * 1-5",
    "timezone": "Asia/Shanghai"
  }
}
JSON
```

Configuration fields included in an update request replace their current values; pass an empty array `[]` to clear `resources` or `initial_events`. Passing an empty string for `agent_version` selects the latest agent version at fire time. `notification` replaces the current configuration only if it appears in the request: pass an empty object `{}` to remove notifications, or omit the field to keep the existing configuration. Changing `session_mode` from `reuse_session` to `new_session` causes subsequent runs to create new sessions rather than reusing the previous session. While `session_mode` remains `reuse_session`, modifying `agent_id`, `agent_version`, `environment_id`, or `resources` returns `400` `failed_precondition_error`; to change any of those fields, switch to `new_session` first or create a new trigger. Pausing, resuming, and archiving cannot be done through the update endpoint—use their respective action endpoints instead.

## Archive a trigger

Archive a trigger when you no longer need it:

```bash cURL theme={null}
curl --request POST "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID/archive" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta"
```

```bash CLI theme={null}
kha triggers archive "$TRIGGER_ID"
```

Archiving returns `204`. Once archived, a trigger never fires again, though existing run records are retained and remain queryable. Archiving is irreversible—confirm that you no longer need the schedule before archiving.

## Receive run notifications

> [!NOTE]
>
> This section covers the trigger's own `notification` configuration. To subscribe to changes across multiple types of resources in your project, use [Project Webhooks](https://platform.kimi.ai/docs/hosted-agents/webhooks.md). Trigger run notifications use a `webhook_secret` stored in a vault, whereas Project Webhooks use the `signing_secret` returned when creating or rotating a Webhook configuration. Their subscription scopes, configuration, and signing secret management are independent.

To push run outcomes to your own system, configure `notification` when creating or updating a trigger. On update, include only the fields you want to change:

```bash cURL theme={null}
curl --request PATCH "https://api.moonshot.ai/v1/triggers/$TRIGGER_ID" \
  --header "Authorization: Bearer $KIMI_API_KEY" \
  --header "kimi-api-version: 2026-09-01-beta" \
  --header "Content-Type: application/json" \
  --data '{
    "notification": {
      "url": "https://example.com/webhooks/trigger-runs",
      "secret_vault": {
        "vault_id": "vlt_your_vault_id"
      }
    }
  }'
```

```bash CLI theme={null}
kha triggers update "$TRIGGER_ID" --file - <<'JSON'
{
  "notification": {
    "url": "https://example.com/webhooks/trigger-runs",
    "secret_vault": {
      "vault_id": "vlt_your_vault_id"
    }
  }
}
JSON
```

`url` is the HTTPS endpoint that receives notifications. `secret_vault` points to a vault containing an `environment_variable` credential named `webhook_secret`, which is used to sign notifications; the credential value is read only at delivery time, so configuring `notification` validates only the vault reference, not whether the `webhook_secret` credential exists. `url` and `secret_vault` must be set together—providing only one is rejected (`400` `invalid_parameter_error`). On update, pass an empty object `{}` to clear the notification configuration, or omit `notification` to retain the current configuration.

Once configured, the platform sends HTTPS POST notifications to that URL for each **scheduled** run (manual runs do not send notifications). There are four notification event types: `trigger_run.started` (run started), `trigger_run.succeeded` (run completed), `trigger_run.failed` (run failed), and `trigger.paused` (an unrecoverable run error caused the trigger to transition automatically from `active` to `paused`). A scheduled run typically delivers two run notifications: `trigger_run.started` at the beginning and `trigger_run.succeeded` or `trigger_run.failed` at the end. Skipped runs do not emit those run events, though `trigger.paused` may still be delivered if the trigger is paused automatically; see [Pause and resume](#pause-and-resume) above. Notifications are delivered at least once; see the delivery policy and deduplication guidance below.

The notification body is a compact JSON envelope carrying only the event type and resource ID. For `trigger_run.*` events, `data.id` is the run ID (`trun_` prefix); query the run history endpoint for details. For `trigger.paused`, `data.id` is the trigger ID (`trig_` prefix); retrieve the trigger to inspect `paused_reason`:

```json theme={null}
{
  "type": "event",
  "id": "whev_01j3z7k8m2n4p6q8r0s2t4v6x8",
  "created_at": "2026-09-02T01:00:00Z",
  "data": {
    "type": "trigger_run.succeeded",
    "id": "trun_01j3z7k8m2n4p6q8r0s2t4v6x9",
    "scope_ref": "project:your_project_id"
  }
}
```

`id` at the top level is the delivery ID (matching the `Webhook-Id` request header); retries resend the identical body byte for byte.

Every delivery includes the `Webhook-Id`, `Webhook-Timestamp`, and `Webhook-Signature: v1,<base64>` request headers. The signature is the Base64-encoded HMAC-SHA256 digest of `"<Webhook-Id>.<Webhook-Timestamp>.<raw_request_body>"` computed using the `webhook_secret` credential value as the key; receivers should compute and compare the signature in the same way to verify that the notification came from the platform.

The platform delivers notifications on an at-least-once basis, making at most 3 attempts per event (including the initial attempt). Any `2xx` response is treated as successful delivery; once all attempts are exhausted, the notification is dropped. Receivers should deduplicate deliveries using `Webhook-Id`.

## Next steps

> [card] [Explore Dreams triggers](https://platform.kimi.ai/docs/hosted-agents/dreams.md)
>
> Learn how to configure platform-managed memory maintenance triggers (Dreams policies).

> [card] [Configure multi-agent orchestration](https://platform.kimi.ai/docs/hosted-agents/multiagent-orchestration.md)
>
> Coordinate multiple agents to complete complex tasks.

> [card] [Stream live session events](https://platform.kimi.ai/docs/hosted-agents/event-stream.md)
>
> Subscribe to live events from triggered sessions to track task progress.
