# Operating an app with an agent

### The idea

Once an app is shipped, most of what happens to it is operations: a traffic spike, a client that starts hammering a model, a provider having a bad hour, a paywall that needs another try. Every one of those is a read from your traffic, a decision, and a change to the app's behaviour without a release. The admin API exposes all three, so an agent, a script, or Claude in your terminal can run the loop.

Three things make this work together:

- Reads: the [analytics endpoints](/docs/admin-api-analytics.html) (summary, top clients, tokens, latency, errors, request history) and [app logs](/docs/admin-api-analytics.html#logs).
- Changes: [remote config flags](/docs/admin-api.html#remote-config) the app reads at launch, [router destinations](/docs/admin-api.html#services) (provider and model), rate limits, model overrides, and bans, all on the [admin API](/docs/admin-api.html).
- Correlation: the app's `aiproxy-client-id` is on every request, every log record, and every analytics row, so the agent can follow one device from a symptom to a decision.

Two keys, two jobs: the read-only API key from the account page reads everything and can change nothing, so it is the one to hand an agent for the watching half of the loop. The admin key changes things; give it to the agent only for the changes you have decided it may make, and keep both out of the app.

```
export API="https://dashboard.aiproxy.com/api/v1"
export AUTH="Authorization: Bearer $AIPROXY_ADMIN_KEY"
export P="4f865a6d" # app uuid
```

### Recipe: stand up an app from nothing

Everything an app needs exists before the app is built: the App Attest configuration, a Foundation Models router with the provider and model, a remote config service with its first flags, and a log ingest service. Four calls, then paste the three service URLs into the app.

```
P=$(curl -s "$API/projects" -H "$AUTH" -H "Content-Type: application/json" -d '{"name": "My app"}' | jq -r .uuid)

curl -s "$API/projects/$P/app_attest" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"apple_team_id": "TEAM12345", "bundle_id": "com.example.app"}'

curl -s "$API/projects/$P/services" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"service_type": "foundation_models_router",
       "generation_destination": {"provider": "anthropic", "model": "claude-haiku-4-5", "api_key": "sk-ant-..."}}'
RC=$(curl -s "$API/projects/$P/services" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"service_type": "remote_config"}' | jq -r .api_uuid)
curl -s "$API/projects/$P/services/$RC/remote_config" -H "$AUTH" -X PUT -H "Content-Type: application/json" \
  -d '{"flags": {"paywall_annual_first": false}}'
curl -s "$API/projects/$P/services" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"service_type": "otel_log_ingest"}'

curl -s "$API/projects/$P/services" -H "$AUTH" | jq '.[] | {service_type, service_url}'
```

The first launch of the app against the new app attests once (about a second), and from then on every request, log record, and analytics row carries the device's client ID.

### Recipe: who is hammering tokens?

Request counts hide the expensive client: a hundred long-context calls cost more than a thousand short ones. Sort clients by tokens, look at what the top one is doing, and decide.

```
# 1. The top token consumers in the last hour
curl -s "$API/projects/$P/analytics/top_clients?window=1h&by=tokens&limit=5" -H "$AUTH"

# 2. What is that client actually sending? Paths, statuses, token counts per request.
curl -s "$API/projects/$P/requests?client_id=8C43A0F1-…&limit=50" -H "$AUTH"

# 3a. A bug in a loop: tighten the per-user rate limit on the service while you ship a fix
curl -s "$API/projects/$P/services/e88e2881/rate_limits" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"limit_on": "requests_per_user", "window": "hour", "quantity": 60}'

# 3b. Abuse: ban the client id, and the IP if the id keeps changing
curl -s "$API/projects/$P/bans" -H "$AUTH" -H "Content-Type: application/json" \
  -d '{"ban_type": "client_id", "value": "8C43A0F1-…", "note": "410k tokens/hour, replaying the same prompt"}'
```

Bans take effect on the next request, and the banned client's later attempts show up in `analytics/errors` as `client_banned`, so the agent can confirm the ban is doing work.

### Recipe: react to a traffic event

A spike is either good news or an attack. Compare the shape of the traffic with a quiet baseline before touching anything.

```
# Now vs. the same span yesterday
curl -s "$API/projects/$P/analytics/summary?window=15m" -H "$AUTH"
curl -s "$API/projects/$P/analytics/summary?from=$(( $(date +%s) - 86400 - 900 ))&until=$(( $(date +%s) - 86400 ))" -H "$AUTH"

# Is it many new clients (launch, feature) or a few clients (abuse)?
curl -s "$API/projects/$P/analytics/top_clients?window=15m&limit=10" -H "$AUTH"
curl -s "$API/projects/$P/analytics/top_countries?window=15m" -H "$AUTH"

# Is the provider keeping up?
curl -s "$API/projects/$P/analytics/latency?window=15m&metric=ttft" -H "$AUTH"
curl -s "$API/projects/$P/analytics/errors?window=15m" -H "$AUTH"
```

Then act at the right layer: a per-IP rate limit for a scraper, a model override or a router destination change to a cheaper or healthier model while a provider is degraded, a remote config flag to turn an expensive feature off for everyone until the spike passes.

```
# Move a Foundation Models router to a cheaper model for the duration; no app update
curl -s "$API/projects/$P/services/c27411bd" -H "$AUTH" -X PATCH -H "Content-Type: application/json" \
  -d '{"generation_destination": {"model": "claude-haiku-4-5"}}'

# Turn a feature off with a flag the app reads at launch
curl -s "$API/projects/$P/services/4995ea8e/remote_config" -H "$AUTH" -X PUT -H "Content-Type: application/json" \
  -d '{"flags": {"image_generation": false}}'
```

### Recipe: run an experiment and read the result

A paywall variant, a new creative, a different prompt: make it a string flag with weighted variants, have the app log what it did as an event with attributes, then read the events back per variant. No release, and the whole loop is four calls.

```
# 1. Split the audience 50/50; pin your own device into the new variant to check it first
curl -s "$API/projects/$P/services/$RC/remote_config" -H "$AUTH" -X PUT -H "Content-Type: application/json" \
  -d '{"flags": {"paywall": {"kind": "string", "value": "monthly_first",
        "variants": [{"value": "annual_first", "weight": 50}, {"value": "monthly_first", "weight": 50}],
        "overrides": {"'$MY_PHONE'": "annual_first"}}}}'

# 2. Confirm what a given device will see before it launches
curl -s "$API/projects/$P/services/$RC/remote_config/evaluate?client_id=$MY_PHONE" -H "$AUTH"

# 3. The app reads flags.string("paywall") at launch (AIProxyRemoteConfig) and logs the outcome
# (AIProxyLogs): #logAt(.info, "paywall shown", event: "paywall.result", attributes: ["variant": variant, "outcome": "purchased"])

# 4. Count outcomes per variant; client_id on each record lets you count devices, not lines
curl -s "$API/projects/$P/services/$LOGS/logs?event=paywall.result&attr=variant:annual_first&attr=outcome:purchased&limit=500" -H "$AUTH"
curl -s "$API/projects/$P/services/$LOGS/logs?event=paywall.result&attr=variant:monthly_first&attr=outcome:purchased&limit=500" -H "$AUTH"

# 5. Decide: move the winner to 100 percent, or try the next variant
curl -s "$API/projects/$P/services/$RC/remote_config" -H "$AUTH" -X PUT -H "Content-Type: application/json" \
  -d '{"flags": {"paywall": {"variants": [], "value": "annual_first"}}}'
```

Assignment is a stable hash of the flag key and the device's client id, so a device stays in its variant across launches and a rollout from 10 to 50 percent keeps the first 10 percent in. A boolean flag with `rollout_percent` is the same idea for a feature switch: expose it to 10 percent, watch `analytics/errors` and the event logs, widen. Records are kept for 30 days, long enough to compare against last week's variant.

In development the same loop runs against a local proxy and a phone on your desk: an agent sets the flag, launches the app with a run id in its environment, and fetches the events, without opening the dashboard.

### Recipe: watch a release

```
# Error and latency trend since the release went live, in five-minute buckets
curl -s "$API/projects/$P/analytics/series?metric=provider_errors&window=6h&bucket=300" -H "$AUTH"
curl -s "$API/projects/$P/analytics/series?metric=rejected&window=6h&bucket=300" -H "$AUTH"

# The failed requests themselves, newest first
curl -s "$API/projects/$P/requests?status=failed&limit=100" -H "$AUTH"

# And what the affected devices logged around that time (WARN and above)
curl -s "$API/projects/$P/services/d632dce0/logs?min_severity=13&limit=200" -H "$AUTH"
```

### What to give the agent

- The read-only API key for reads, the admin API key only if it may make changes, and the app UUID.
- The service UUIDs of the remote config and log ingest services, so it does not have to discover them (it can, with `GET /projects/:uuid/services`).
- A policy for what it may change on its own (flags, rate limits) and what it should propose (bans, destination changes), since every change here is live for real users.
- This page and the two API references, or the Markdown versions linked from [llms.txt](/llms.txt).


