{
  "id": 11887074,
  "title": "FastAPI Request/Response Logging for AI Feature Attribution: Tracking Which Claude Call Cost Which Tenant",
  "url": "https://urgent.news/2026/10/04/fastapi-request-response-logging-for-ai-feature-attribution-tracking",
  "topic": "ai",
  "section": "AI",
  "published": "2026-10-04T08:55:55.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/uaslimcreate/fastapi-requestresponse-logging-for-ai-feature-attribution-tracking-which-claude-call-cost-which-pna"
  },
  "original_language": "en",
  "account": "FastAPI logging system helps track which Claude API call cost which tenant when a SaaS starts charging for AI features. A customer emailed about a $340 increase in their bill after 50 Claude API calls were made on a specific day. The original tutorials only show how to log requests and responses separately, which is not sufficient for AI attribution. To solve this issue, a request-scoped context must be used to persist across all async operations, combined with a structured logging format that includes the correlation ID in every log line.\n\nThe implementation involves using Python's contextvars to store request metadata, as it's thread-safe and async-safe unlike thread-local storage. A RequestContext dataclass is defined with fields like request_id, tenant_id, user_id, and endpoint. A get_context and set_context function is created to manage the request context.\n\nA middleware is implemented in the FastAPI application using the @app.middleware decorator. It extracts the tenant_id and user_id from the JWT or header, generates a unique request_id using uuid, and sets the RequestContext with the extracted values. The start_time is recorded and the middleware awaits the call_next function to process the request.\n\nIf the request completes successfully, the response is returned along with the request_completed log entry, including the correlation ID, tenant_id, status code, duration in milliseconds, and endpoint. In case of an exception, the duration is calculated and the error details are logged as well. This logging system ensures that each request is properly attributed to the correct tenant, enabling better debugging and cost tracking for AI features.",
  "summary": "FastAPI Request/Response Logging for AI Feature Attribution: Tracking Which Claude Call Cost Which Tenant When your SaaS starts charging tenants for AI features, you enter a new debugging hell. A customer emails: \"Why did my bill jump $340 last Tuesday?\" You grep your logs. You find 50 Claude API calls that day. But which endpoint triggered them? Which user? Did they hit a bug that looped…",
  "key_points": [
    "FastAPI logging system tracks Claude API calls per tenant",
    "Request-scoped context preserves async operations",
    "Middleware logs requestid, tenantid, and response details"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}