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

# Scale before a traffic peak

> An API Policy Token example for an agent that adds instances before a known traffic peak and removes them after, within a floor and a ceiling, and cannot change anything else.

A reliability agent that adds instances before a known traffic peak, such as a launch, a sale or a marketing email, and removes them after. It can change the number of instances of listed production applications between a floor and a ceiling. CPU, memory and branch stay as they are.

**Level:** Reliability. See all the [agent policy examples](https://www.qovery.com/docs/configuration/organization/policy-examples/overview.md).

## What the agent does

1. Reads the current instances and the status of each listed application.
2. Before the peak, raises the minimum and maximum number of instances.
3. Redeploys the applications and checks they are healthy.
4. After the peak, sets the instances back.

## What the policy allows

> [card] The agent can
>
> * Find the organization, project and environment by name
> * Read the production environment, except environment variables
> * Set the minimum and maximum instances of the listed applications, between the floor and the ceiling
> * Redeploy the listed applications

> [card] The agent cannot
>
> * Go above the ceiling, set an unlimited maximum, or go below the floor
> * Change CPU, memory or the Git branch
> * Deploy a new version
> * Change environment variables, delete anything, or touch other applications

Anything the policy does not allow is refused with `401 Unauthorized`.

## The policy

Copy it, then replace each `REPLACE_WITH_...` value with a real ID from your organization.

```rego scale-before-traffic-peak.rego theme={null}
default allow := false

organization_id := "REPLACE_WITH_ORGANIZATION_ID"
project_id := "REPLACE_WITH_PROJECT_ID"
production_environment_id := "REPLACE_WITH_PRODUCTION_ENVIRONMENT_ID"

# The applications the agent may scale, with their current CPU, memory and branch, and the
# highest number of instances the agent may set.
applications := {
	"REPLACE_WITH_API_APPLICATION_ID": {"cpu": 1000, "memory": 2048, "branch": "main", "floor": 2, "ceiling": 12},
	"REPLACE_WITH_WEB_APPLICATION_ID": {"cpu": 500, "memory": 1024, "branch": "main", "floor": 2, "ceiling": 8},
}

allow if cli_lookup
allow if read_production
allow if scale
allow if redeploy

# Let the Qovery CLI find the organization, project and environment by name.
cli_lookup if {
	input.request.method == "GET"
	input.request.path in {
		["api", "organization"],
		["api", "organization", organization_id],
		["api", "organization", organization_id, "project"],
		["api", "project", project_id],
		["api", "project", project_id, "environment"],
	}
}

# Read production: services, status, deployment history and logs.
read_production if {
	input.request.method in {"GET", "HEAD"}
	input.qovery_metadata.environment_id == production_environment_id
	not "environmentVariable" in input.request.path
}

# Change the number of instances between the floor and the ceiling.
# CPU, memory and branch must stay as they are.
scale if {
	input.request.method == "PUT"
	input.request.path[1] == "application"
	count(input.request.path) == 3
	app := applications[input.request.path[2]]
	body := input.request.body
	body.min_running_instances >= app.floor
	body.max_running_instances >= body.min_running_instances
	body.max_running_instances <= app.ceiling
	body.cpu == app.cpu
	body.memory == app.memory
	body.git_repository.branch == app.branch
}

# Redeploy a scaled application so the new values apply.
redeploy if {
	input.request.method == "POST"
	input.request.path[1] == "application"
	applications[input.request.path[2]]
	input.request.path[3] == "redeploy"
	count(input.request.path) == 4
}
```

| Value | What to put |
| - | - |
| `organization_id` | your organization |
| `project_id` | the project |
| `production_environment_id` | the production environment |
| `applications` | each application the agent may scale, with its current CPU, memory and branch, and the floor and ceiling for its instances |

You can copy the IDs from the Console URL, or list them with `qovery project list`, `qovery environment list` and `qovery application list`.

## Create the token

> [tab] With the coding agent
>
> In a session signed in as an organization Owner or Admin, ask:
>
> ```
> /qovery-policy-token for a scaling agent on production: read production, and change only the min and max instances of the api application (2 to 12) and the web application (2 to 8), then redeploy them. CPU, memory and branch must not change. Nothing else.
> ```
>
> The `qovery-policy-token` skill finds the IDs, writes a policy like the one above, tests it locally and against the API, then creates the token.

> [tab] In the Console
>
> 1. Open your organization settings, then **Token API**.
> 2. In **Policy API Token**, click **Add new**.
> 3. Enter a name and a description, and paste the policy with your IDs.
> 4. Copy the token (`sk-qov-01-...`). It is shown once.

## Prompt for the agent

The agent runs with the [Qovery Agent Skills](https://www.qovery.com/docs/getting-started/quickstart/agent-skills.md) and the [Qovery CLI](https://www.qovery.com/docs/cli/overview.md), in Claude Code, Codex, Cursor or any agent that supports skills. Give it the token through the environment, never in the prompt:

```bash theme={null}
export QOVERY_CLI_ACCESS_TOKEN="sk-qov-01-..."
```

Then give the agent its task:

```
We send a marketing email to 400,000 users on Tuesday at 10 AM Paris time.

On Tuesday at 9:30 AM, in the Qovery project "shop", environment "production":
- set the "api" application to a minimum of 6 and a maximum of 12 instances,
- set the "web" application to a minimum of 4 and a maximum of 8 instances,
- redeploy both and check they are healthy.

On Tuesday at 2 PM, set both back to a minimum of 2 and a maximum of 4,
and redeploy. Report each change.
```

Every call the agent makes appears in the [audit logs](https://www.qovery.com/docs/getting-started/security-and-compliance/audit-logs.md) as `policy:<token-id>:<token-name>`.

## Good to know

* Autoscaling already adds instances when CPU or memory goes up. This agent is for peaks you know in advance, so the instances are ready before the traffic arrives.
* The policy pins CPU, memory and branch to the values you write in it. If you change them later, create a new token with the new values.

## Related

> [card] [All agent policy examples](https://www.qovery.com/docs/configuration/organization/policy-examples/overview.md)
>
> Every use case, from read only to production changes.

> [card] [API Policy Token (Preview)](https://www.qovery.com/docs/configuration/organization/api-policy-token.md)
>
> How policies are evaluated, the input they see, and the limits.


## Related topics

- [Agent policy examples](https://www.qovery.com/docs/configuration/organization/policy-examples/overview.md)
- [Qovery-Managed Scaleway Kapsule](https://www.qovery.com/docs/configuration/integrations/kubernetes/scaleway/managed.md)
- [Scaleway IAM Permissions Reference](https://www.qovery.com/docs/getting-started/security-and-compliance/scaleway-iam-permissions.md)
- [Install Qovery on Scaleway](https://www.qovery.com/docs/getting-started/installation/scaleway.md)
- [Service Advanced Settings](https://www.qovery.com/docs/configuration/service-advanced-settings.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.