Use your own AI keys #

View this page as Markdown

On this page

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, declare the provider as an 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, 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.

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 (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 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).
  • Moves only the calls you name onto your key. Every other AI call stays on the platform's AI route.
  • 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 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 describes. These options keep its value out of chat, code, and the application's container. The 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 lists each field. 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 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 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. 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 for other platform errors.

Call an external API with an API key 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. 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.