Optimization

How do I split AI spend by environment and compare prompt versions on cost?

Tag calls with X-Cost-Environment to see production vs staging vs CI spend and budget each one; tag them with X-Cost-Variant to compare prompt versions or models side by side on real cost, tokens, latency and errors.

In the app: Environments · every planOpen in app →No account? Start free

Environments

Send X-Cost-Environment on every call. Use any names you like; common ones are production, staging, ci, eval and dev.

import os
from openai import OpenAI
 
client = OpenAI(
    base_url="https://api.cognocient.com/v1",
    api_key=os.environ["COGNOCIENT_KEY"],
    default_headers={"X-Cost-Environment": os.environ.get("APP_ENV", "production")},
)

With the Python wrapper, pass cognocient_environment="staging" instead.

Observe → Environments shows spend, share, calls, tokens and error rate for each environment, a daily stacked chart, and how much of your spend is outside production. Calls without the header are grouped as (untagged).

Budget an environment

Create a budget with scope Environment and the environment name (for example ci). It behaves like any other budget: alert, degrade or block, but only for calls that carry that environment. A common setup is a hard block budget on ci and eval so a runaway test loop cannot eat into production's headroom.

An environment budget matches on the tag the caller sends. A call without X-Cost-Environment is not counted against any environment budget, but it still counts against global, feature, department, user and key budgets.

Variants (Experiments)

Tag each call with the version it ran under:

variant = "prompt-v2" if bucket(user_id) else "prompt-v1"   # your own split
client.chat.completions.create(
    model="gpt-4o-mini",
    messages=build_messages(variant),
    extra_headers={"X-Cost-Variant": variant, "X-Cost-Feature": "support-reply"},
)

With the Python wrapper, pass cognocient_variant="prompt-v2".

Intelligence → Experiments (Growth) compares the variants side by side:

MetricNotes
Avg cost per call, total spend, callsFrom your own traffic, not list prices
Avg prompt / completion tokensShows whether a prompt change really shrank context
p50 / p95 latency, error rate
Outcomes reached, spend per outcomeOnly if you record outcomes. An outcome counts for a variant when any call it was matched to carries that variant. Spend per outcome is the variant's total spend divided by those outcomes.

Pick a baseline and every other column shows its change against it. Filter by feature, environment or outcome. Variants with fewer than 30 calls are flagged as a small sample.

You can also compare by model (did the cheaper model, or the Auto Router, really save money on this feature?) or by environment.

The same comparison is available to AI assistants through the MCP server (compare_variants).

On this page