aggregator-key-cost-observability · EN · 2026-10-21

Per-Request Cost Tags: Annotating Calls on One Key So You Can Attribute Spend by Feature

Learn how to attribute API spend to individual features when using a single aggregator key. This guide explains per-request cost tagging: attaching custom metadata to each call, logging locally, and reconstructing per-feature costs from your own logs. It covers implementation patterns, computation methods, and best practices for accurate cost allocation.

Why Per-Feature Cost Attribution Matters

When you use a single API key across multiple features—such as a chatbot, a summarization tool, and a code assistant—the aggregator bills you at the key level. That makes it hard to know which feature is driving costs. Per-request cost tagging solves this by letting you attach your own metadata to each call and then reconstruct spend from local logs.

How the Aggregator Bills

The aggregator charges you based on total usage across all requests made with your key. It does not natively break down costs by feature, user, or any custom dimension. However, every request includes model, token counts, and other details that you can capture and log locally.

Designing Your Tagging Schema

Before you start logging, decide on a consistent tagging schema. Common dimensions include:

  • Feature name (e.g., chat, summarize, code-review)
  • User ID or tenant ID (if you need per-user attribution)
  • Environment (e.g., production, staging)
  • Experiment or version (e.g., prompt-v2)

Keep the schema simple and stable. Use short, machine-readable keys and values.

Attaching Metadata to Requests

The aggregator API typically accepts a metadata field in the request body where you can include arbitrary key-value pairs. For example:

{
  "model": "claude-3-sonnet",
  "messages": [...],
  "metadata": {
    "feature": "chat",
    "user_id": "u12345",
    "env": "production"
  }
}

Check the API documentation for the exact field name and any size limits. Some providers use user or tags instead of metadata. Always verify that the aggregator passes this metadata through to your logs or that you capture it on your side.

Capturing Cost Data Locally

You need to log enough information to compute cost per request. At minimum, capture:

  • Timestamp
  • Model name
  • Input tokens
  • Output tokens
  • Your metadata tags

If the aggregator returns token counts in the response, extract them. If not, you may need to estimate token counts using a tokenizer library. Store these logs in a structured format—JSON lines, a database, or a logging service—that supports querying.

Reconstructing Per-Feature Cost

Once you have logs, compute cost per request using the aggregator's pricing (official price × 1.3 for users). Then aggregate by your tags.

Example calculation

For each log entry:

  1. Look up the official price per token for the model (input and output rates may differ).
  2. Multiply by 1.3 to get your effective rate.
  3. Compute cost = (inputtokens × inputrate × 1.3) + (outputtokens × outputrate × 1.3).
  4. Sum costs grouped by feature tag.

This gives you a clear picture of which feature consumes the most budget.

Handling Contributors and Credits

If you are a key contributor, you receive credits at official price × 1.1 (or × 1.2 for premium models). When reconstructing costs, adjust your calculation accordingly if you need to track net spend or credit usage. The same tagging approach applies.

Best Practices

  • Be consistent: Use the same tag keys and values across all features.
  • Log early: Capture metadata at the point of request creation, not after the fact.
  • Validate tags: Ensure tags are present and correctly formatted to avoid gaps in attribution.
  • Monitor costs regularly: Set up dashboards or alerts based on your aggregated logs.
  • Respect privacy: Avoid logging sensitive user data in tags; use anonymized IDs.

Tools and Automation

You can automate cost reconstruction with a simple script that parses your logs, applies pricing, and outputs a summary. For larger scale, consider a time-series database or a cost management tool that ingests your logs.

Conclusion

Per-request cost tagging is a lightweight but powerful technique to gain visibility into feature-level spend when using a single aggregator key. By attaching metadata, logging locally, and computing costs from token counts, you can make informed decisions about resource allocation and optimization.