How-to Guides

How do I set up MCP agent attribution in Cognocient?

Add three headers to Claude MCP or LangGraph agent calls to get a full workflow cost tree — which tool called what, how much each step cost, and which agent drove the spend.

Goal: Replace "my agent cost $180 yesterday" with "web-search MCP: $94 · code-executor: $62 · document-reader: $24" — a full breakdown of which tool call, agent step, and handoff drove the cost.

Time: 15 minutes. Workflow tree appears in the dashboard after the first instrumented run.

Prerequisite: Cognocient proxy already connected. See Quickstart if not.


Step 1 — Add the three MCP attribution headers

All three headers work alongside your existing X-Cost-Feature and X-Cost-Run-ID headers. Add them to the calls made within your agent's tool execution logic.

HeaderWhat it tagsExample
X-Cost-Parent-Run-IdThe parent workflow run (shared by all steps)run-4a7c2b1e-...
X-Cost-MCP-ServerThe MCP server or tool making this callweb-search
X-Cost-Agent-HandoffWhether this is a sub-agent handofftrue

Step 2 — Instrument a Claude + MCP workflow

The most common pattern: Claude as orchestrator, MCP servers as tools. Pass X-Cost-Parent-Run-Id on the orchestrator call and X-Cost-MCP-Server on each tool call.

import uuid
import anthropic
 
client = anthropic.Anthropic(
    api_key="sk-cog-YOUR-PROXY-KEY",
    base_url="https://api.cognocient.com",
)
 
# Map tool names to MCP server identifiers
TOOL_TO_MCP = {
    "web_search":    "web-search",
    "read_document": "document-reader",
    "execute_code":  "code-executor",
}
 
async def run_research_agent(query: str):
    run_id = f"run-{uuid.uuid4()}"   # unique per execution
 
    # Orchestrator call — no MCP server header (this is the main agent)
    response = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        system="You are a research agent. Use tools to answer the query.",
        messages=[{"role": "user", "content": query}],
        tools=[web_search_tool, document_tool, code_tool],
        extra_headers={
            "X-Cost-Feature":       "research-agent",
            "X-Cost-Parent-Run-Id": run_id,
        },
    )
 
    # Execute each tool call — tag with MCP server
    for block in response.content:
        if block.type == "tool_use":
            mcp_server = TOOL_TO_MCP.get(block.name, block.name)
 
            tool_result = client.messages.create(
                model="claude-sonnet-4-6",
                max_tokens=512,
                messages=[
                    {"role": "user", "content": query},
                    {"role": "assistant", "content": response.content},
                    {"role": "user", "content": [{"type": "tool_result", "tool_use_id": block.id, "content": execute_tool(block)}]},
                ],
                extra_headers={
                    "X-Cost-Feature":       "research-agent",
                    "X-Cost-Parent-Run-Id": run_id,
                    "X-Cost-MCP-Server":    mcp_server,   # ← key header
                },
            )

Step 3 — Instrument a LangGraph multi-agent handoff

For supervisor/sub-agent patterns, add X-Cost-Agent-Handoff: true on sub-agent calls so Cognocient shows handoffs separately in the workflow tree.

import OpenAI from 'openai';
import { v4 as uuidv4 } from 'uuid';
 
const openai = new OpenAI({
  apiKey: 'sk-cog-YOUR-PROXY-KEY',
  baseURL: 'https://api.cognocient.com/v1',
});
 
async function supervisorAgent(task: string) {
  const runId = `run-${uuidv4()}`;
 
  // Supervisor call
  const supervisor = await openai.chat.completions.create(
    { model: 'gpt-4o', messages: [{ role: 'user', content: task }] },
    {
      headers: {
        'X-Cost-Feature':       'multi-agent-pipeline',
        'X-Cost-Parent-Run-Id': runId,
      },
    }
  );
 
  // Sub-agent handoff — tag with MCP server + handoff flag
  const subAgent = await openai.chat.completions.create(
    {
      model: 'gpt-4o-mini',
      messages: [{ role: 'user', content: supervisor.choices[0].message.content! }],
    },
    {
      headers: {
        'X-Cost-Feature':       'multi-agent-pipeline',
        'X-Cost-Parent-Run-Id': runId,
        'X-Cost-MCP-Server':    'research-agent',   // sub-agent identity
        'X-Cost-Agent-Handoff': 'true',              // marks this as a handoff
      },
    }
  );
 
  return subAgent;
}

Start with just X-Cost-MCP-Server

If you only add one header, add X-Cost-MCP-Server. Cost by MCP server is immediately the most actionable insight — it tells you which tool to optimise first without needing the full workflow tree.

Step 4 — View the workflow tree in the dashboard

Go to Dashboard → Agent Workflows. After the first instrumented run you'll see:

  • Cost by MCP server — bar chart showing spend per tool (web-search, code-executor, etc.)
  • Workflow tree per run — each step, which MCP server called it, tokens, cost, and latency at each node
  • Most expensive runs — ranked by total cost with anomaly flags
  • Handoff cost — what fraction of total cost came from agent-to-agent handoffs vs. direct calls

Example workflow tree in the dashboard:

research-agent (supervisor)          $0.21  214 tokens
├── web-search (tool call)            $0.42  890 tokens  [MCP: web-search]
│   └── result synthesis              $0.09  180 tokens
├── document-reader (tool call)       $0.06  312 tokens  [MCP: document-reader]
└── final-response                    $0.05   98 tokens
Total run cost: $0.83

Related: MCP / A2A Attribution · Track Cost Per Agent Run · Debug Runaway Agent Loops

On this page