Skip to main content

Integrate MarginFront with Anthropic

This recipe shows how to add MarginFront usage tracking to an app that calls the Anthropic API (Claude models). Same pattern as the OpenAI recipe, but Anthropic names its fields slightly differently. What this does for you: You call Claude. MarginFront records the model, token count, and customer. You see costs on your dashboard. If you have a pricing plan, your customer gets billed automatically.

Prerequisites

  1. A MarginFront API key (mf_sk_...). Get one from Build > API keys in the dashboard.
  2. An Anthropic API key.
You don’t need to create the agent or signal first. When you fire your first event with a new agentCode or signalName, MarginFront creates them automatically. The same goes for customerExternalId. You can rename and enrich any of them in the dashboard later.

Complete working example

Copy-paste and run. Receives a question from a customer, asks Claude for an answer, sends the answer back, and tells MarginFront what happened.

Where the data comes from: field mapping

The two cache fields are optional — only send them if you use prompt caching. See Prompt caching below.

The naming difference from OpenAI

This is the one thing that trips people up: MarginFront always uses inputTokens and outputTokens (camelCase). You just need to read from the right field on the provider side.

Prompt caching

If you use Claude prompt caching, pass the cache token counts so MarginFront prices cached traffic at the cheaper cache rate instead of folding it into the full input rate. Both fields are optional — leave them off and cache contributes nothing, exactly as before. Anthropic returns two cache counts on response.usage:
Read-back of these counts depends on your Claude API version having prompt caching enabled. If a response has no cached tokens, the fields come back as 0 — pass them through as-is or omit them.

What happens if MarginFront is down?

Same as OpenAI — nothing bad. The SDK runs in fire-and-forget mode by default:
  • MarginFront unreachable? Event goes into a local retry buffer. Retries automatically.
  • Validation error? Warning logged, event dropped. Your customer still got their answer.
  • Your server crashes? That one event is lost. Acceptable for most use cases.
MarginFront never blocks your agent. Your customer always gets their answer.

Streaming responses

Anthropic’s streaming works differently from OpenAI. The message_stop event includes the final message with usage data:
Unlike OpenAI, you don’t need to pass any special option to get usage data — Anthropic always includes it in the final message.

Extended thinking (Claude models with thinking enabled)

If you use Claude’s extended thinking feature, the input and output token counts in response.usage already include thinking tokens. You don’t need to do anything special — just pass them as-is:

Using multiple Claude models

If your agent picks different models for different tasks (e.g., Haiku for fast summaries, Sonnet for detailed analysis), just pass the model that was actually used:
MarginFront looks up the cost per model automatically. A Haiku call costs less than a Sonnet call, and your dashboard shows the difference.

Error handling (if you want more control)

Turn off fire-and-forget to catch tracking errors:

Auto-provisioning note

If you log an event for a customerExternalId or agentCode that MarginFront hasn’t seen before, it will auto-create a minimal customer or agent record. Handy for prototyping, but can mask typos. If events show up under an unexpected name, check your IDs in the dashboard.

Reading back what you earned

You sent events. Now you want numbers: revenue, cost, margin, MRR. Three canonical paths, all point to the same data.

Via SDK (inside your app)

Via REST (from any language or curl)

Via MCP (from an AI agent like Claude, Cursor, or Copilot)

If you’ve connected MarginFront’s MCP server to your AI coding assistant, the agent can call canonical analytics tools directly:
All three paths return the same canonical numbers. Pick whichever fits your workflow.