---
title: Use your own AI keys
description: Run AI calls with your own provider key. Choose which calls use your key and which use the Turn Zero AI route.
slug: /guides/use-your-own-ai-keys
order: 11
visibility: public
redirects_from: []
---

# Use your own AI keys

Prompt:

> Use my own OpenAI key for the chat feature. I stored it on the Turn Zero dashboard as OPENAI_KEY.

Also works:

- "Run the summaries on Claude with my Anthropic key, stored as ANTHROPIC_KEY, and leave the title calls on the platform's AI route."
- "Use my stored Google key for the summaries."

Store your provider key as a [secret](/cloud/getting-started/store-a-secret/), declare the provider as an [upstream](/cloud/concepts/glossary/#declared-upstream), and send requests through it. You choose per call whether to use your key or the platform's AI route. Read the platform’s rates on the [pricing page](https://dev.turnzero.ai/pricing), or call `read_rate_card`. An application has no monthly AI allowance. Platform-route calls draw the account's balance at the provider's published price plus the markup on the [pricing page](https://dev.turnzero.ai/pricing).

## What your AI does

- Confirms the key's name with `list_secrets`. If the key is not stored yet, it follows the `store-key` [skill](/cloud/concepts/glossary/#skill) (`read_context`, id `skill:store-key`).
- Declares the provider as an upstream and names the stored key. If the manifest already declares that upstream, your AI assistant updates the manifest. Otherwise it uses `declare_outbound_api`. [Call an external API with an API key](/cloud/guides/call-an-external-api-with-an-api-key/) shows how.
- Where your code uses a provider's own software development kit (SDK), leaves it unchanged and declares the upstream with `sdk_settings` ([An unchanged SDK](/cloud/guides/call-an-external-api-with-an-api-key/#an-unchanged-sdk)).
- Moves only the calls you name onto your key. Every other AI call stays on the platform's [AI route](/cloud/concepts/glossary/#ai-calls).
- Deploys, then reads `read_status` until the deploy ends.
- Asks you the name you stored the key under, and which calls move to it, where the prompt does not say.

## What you need

- An API key from your AI provider. Anthropic, OpenAI, Google, and xAI are supported. Your application names the provider and the model on each call.
- The key stored on the Turn Zero dashboard under a name, such as `OPENAI_KEY`, as [Store a secret](/cloud/getting-started/store-a-secret/) shows. Nobody can read a stored key back, your AI included.

## Before your AI starts

**For your AI:** Check the following before starting.

- Name the provider and the model on each call. Anthropic, OpenAI, Google, and xAI are supported.
- Choose the route on each call. A call through the person's upstream runs on their stored key. Your AI provider bills you for model use. Turn Zero charges data transfer for the request body bytes its gateway sends to your provider. Turn Zero adds no markup to your provider's charge. A call on the platform's AI route draws the account's balance at the provider's published price plus the pricing page's markup. A stored key changes no other call's route.
- Move only the calls the person names onto their stored key. Leave every other AI call where it is.

## Steps

### 1. Store your key

Store the key in the vault through the dashboard or a local file, as [Store a secret](/cloud/getting-started/store-a-secret/) describes. These options keep its value out of chat, code, and the application's container. The [egress gateway](/cloud/concepts/glossary/#egress-gateway) adds the stored key before forwarding each request to the provider. Replacing it under the same name takes effect on subsequent calls without a redeploy.

### 2. Declare the provider's upstream

Declare one upstream for each provider. The upstream names the provider's API address, the header the key goes in, and the stored key's name. [Declare the upstream](/cloud/guides/call-an-external-api-with-an-api-key/#2-declare-the-upstream) lists each field. [An unchanged SDK](/cloud/guides/call-an-external-api-with-an-api-key/#an-unchanged-sdk) gives the settings the OpenAI and Anthropic SDKs read.

If the manifest already declares the upstream by name, update that entry and submit the manifest. If `declare_outbound_api` returns `manifest_owned_field`, make the change in the manifest. Use `declare_outbound_api` to change an upstream declared separately.

A running copy gets a newly declared SDK base URL and any optional egress-key settings on its next deploy, promote, or restart. If you only replace the provider key under the same stored name, the gateway uses it on subsequent calls without a redeploy.

### 3. Call the model

Your code calls the provider's own API paths through the upstream. It can use the [Egress](/cloud/backend-packages/egress/) package's client, or the provider's SDK unchanged. The gateway returns the provider's status and response body unchanged. Your AI provider bills you for model use. Turn Zero charges data transfer for the request body bytes its gateway sends to your provider. Turn Zero adds no markup to your provider's charge.

### 4. Choose the route for each call

To use the platform’s key, call the [AI Generation](/cloud/backend-packages/ai-generation/) package. To use your key, send the request through your upstream. Choose the route for each call; storing a key changes no other call’s route.

Calls on the platform’s key draw the account’s balance at the provider’s published price plus the pricing page’s markup. Your AI provider bills you for model use. Turn Zero charges data transfer for the request body bytes its gateway sends to your provider. Turn Zero adds no markup to your provider's charge.

During an account pause, calls on your own key are refused too. Follow [Handle an account pause](/cloud/guides/call-an-ai-model/#5-handle-an-account-pause). Use `tzcloud_read_usage` for the application’s AI spend this month, and `read_balance` for the account’s balance and monthly lines.

## Expected result

The calls you moved run on your key. Your AI provider bills you for model use. Turn Zero charges data transfer for the request body bytes its gateway sends to your provider. Turn Zero adds no markup to your provider's charge. The platform counts each call per upstream in your application's usage.

## Refusals

See the [refusals reference](/cloud/reference/refusals/) for other platform errors.

[Call an external API with an API key](/cloud/guides/call-an-external-api-with-an-api-key/#refusals) lists the other refusals of the gateway, the platform route that adds your key to each call. A refusal from the provider itself, such as an invalid key or the provider's own rate limit, comes back unchanged.

| Refusal | Status | Cause | Remedy |
| --- | --- | --- | --- |
| `credential_not_in_custody` | 409 | No key is stored under the `credential_name` for this environment or for the account. At a call, `detail` says when the key is stored for the other environment only. | Store the key under that name with `store_secret`, for this environment or for the account, then retry. |
| `undeclared_upstream` | 404 | Your account declares no upstream by that name. | Correct the name if the detail rejects its form. Check it with `list_outbound_apis`. If it is absent, use `declare_outbound_api` before sending the request again. |
| `upstream_unreachable` | 502 | The gateway could not reach the upstream or finish reading its response. The `detail` gives the error code. | Check the base URL and the upstream’s status. If resolution fails, correct the destination you manage or report a platform-managed origin. For other transport errors, the upstream may have acted before its response was lost. Retry a read if needed. For a write, retry only after confirming it had no effect or by reusing the same idempotency key where the upstream supports it. Treat a cut-off stream, including one that ends with `egress_error`, as incomplete. |
| `egress_daily_bytes_capped` | 429 | The application passed its outbound-byte limit for the UTC day. The body includes `count`, `limit`, and `resets_at`. | Wait for `resets_at`. |
| `account_paused` | 429 | A gateway call met an account pause. | Follow [Handle an account pause](/cloud/guides/call-an-ai-model/#5-handle-an-account-pause). Do not retry the call. |
| `allowance_exhausted` | 429 | One of Turn Zero's own applications on its unlimited plan has used up its AI allowance. | Wait for `resets_at`, or use your own upstream and key. Customer applications have no monthly AI allowance. |

## Related

- [Call an external API with an API key](/cloud/guides/call-an-external-api-with-an-api-key/) declares an upstream and calls it, with samples.
- [Call an AI model](/cloud/guides/call-an-ai-model/) uses the platform's AI route.
- [Egress](/cloud/backend-packages/egress/) is the client for calls on your stored keys.
- [AI Generation](/cloud/backend-packages/ai-generation/) describes the published package.
- [Store a secret](/cloud/getting-started/store-a-secret/) stores and replaces a key.
