Spend Attribution

How do I track AI spend per conversation and per work item?

A session is one complete interaction — a support ticket, a JIRA story, a PR, or an agent task. Sessions let you measure cost per conversation and per work item, the metrics that map to business value and sprint reviews.

A Cognocient session is one complete interaction — a support ticket, a JIRA story, a PR, or an agent task run. Pass a unique X-Cost-Session ID on each turn to measure total cost per conversation and map AI spend to the work that generated it.

MetricValueDetail
Avg session cost$0.18Median across all session types
Longest session142 callsAgent workflow, unbounded loop
Cost reduction34%After context pruning

What is a session?

A session is any group of API calls that you associate with a single logical interaction, passed using the X-Cost-Session header. Cognocient sums all calls in a session to give you a single cost figure for that interaction.

Session: sess_7x9a2b           Cost: $0.42
├── Call 1  intent detection   gpt-4o-mini    $0.001
├── Call 2  context retrieval   gpt-4o-mini    $0.003
├── Call 3  response generation gpt-4o         $0.38
├── Call 4  tone check          gpt-4o-mini    $0.002
└── Call 5  response summary    gpt-4o-mini    $0.034

Without session tracking, you see five separate API calls with no connection between them. With a session ID, you see a single "support ticket handled" event costing $0.42 — a number you can directly compare to the value of resolving that ticket.

How to tag sessions

X-Cost-Session: sess_7x9a2b (required)

A unique identifier for one logical interaction. Generate a UUID or use your existing conversation/ticket ID. All calls within this ID are grouped into one session cost.

import uuid
from openai import OpenAI
 
client = OpenAI(
    api_key="sk-cog-YOUR-PROXY-KEY",
    base_url="https://api.cognocient.com/v1",
)
 
def handle_support_ticket(ticket_id: str, user_message: str):
    # Use your existing ticket ID as the session ID
    session_id = f"ticket_{ticket_id}"
 
    # All calls in this ticket share the same session ID
    intent = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": f"Classify intent: {user_message}"}],
        extra_headers={
            "X-Cost-Feature": "support-chat",
            "X-Cost-Session": session_id,
        },
    )
 
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": user_message}],
        extra_headers={
            "X-Cost-Feature": "support-chat",
            "X-Cost-Session": session_id,   # same ID — grouped together
        },
    )
 
    return response

You almost certainly have an ID you can reuse: ticket ID, conversation ID, order ID, document ID. You don't need to generate new UUIDs — use what you already have.

Analysing sessions in the dashboard

The Sessions view shows a table sortable by cost, call count, duration, and waste detected. Click any session to see the full call tree — every API call in order, with model, token count, and cost for each step.

Sort by cost descending — Your most expensive sessions are at the top. These are the ones worth profiling first — a single session costing 10× the median usually reveals a specific architectural problem.

Filter by feature — Compare session costs within the same feature over time. If average session cost for "support-chat" rises 20% week-over-week, something changed — model, prompt, or user behaviour.

Look at the call count distribution — A healthy session has 2–6 calls. Sessions with 20+ calls usually indicate an agent loop that isn't terminating efficiently — a common source of runaway cost in agentic workflows.

Check the waste column — Sessions flagged with waste have at least one of: retry calls, model mismatch, context bloat, or context starvation. Drill in to see exactly which call triggered the flag.

Session cost as a business metric

Session cost is most powerful when mapped to a business outcome. Once you tag sessions consistently, you can calculate:

Use caseSession = one …Business metric
Customer supportSupport ticket handledAI cost per resolved ticket
Legal / contractsContract drafted or reviewedAI cost per contract
Sales automationOutbound email sequenceAI cost per deal opened
Document processingDocument extracted + classifiedAI cost per document
Code reviewPR review completedAI cost per PR

Connect sessions to outcomes in the Outcomes view. Once you define what counts as a successful outcome for a session type, Cognocient tracks cost per successful outcome separately from cost per failed or abandoned session.

Workstream attribution — JIRA stories, PRs, and agent tasks

When you pass a JIRA story key, PR number, or agent task UUID as the session ID, Cognocient recognises the pattern and surfaces it in the Workstreams dashboard — grouped by sprint work item with total cost, call count, and duration.

# JIRA story attribution
extra_headers={"X-Cost-Feature": "chatbot", "X-Cost-Session": "JIRA-2341"}
 
# GitHub PR attribution
extra_headers={"X-Cost-Feature": "code-review", "X-Cost-Session": "pr-892"}
 
# Agent task execution
import uuid
extra_headers={"X-Cost-Feature": "research-agent", "X-Cost-Session": str(uuid.uuid4())}

This answers the question every engineering manager asks at sprint review: "We spent $X on AI — which stories generated that cost?" See Workstream Attribution and the Workstreams Dashboard for the full view.


Next steps: Live Calls · Outcomes · Waste Detection · Workstream Attribution

On this page