Urgent.News

What's breaking now, across thousands of outlets.

AI

FastAPI Request/Response Logging for AI Feature Attribution: Tracking Which Claude Call Cost Which Tenant

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…

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.

The 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.

A 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.

If 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

How AI Is Making Restaurant Menus Easier to Navigate

The restaurant experience is evolving far beyond the table—now, it begins with the menu. Whether you’re ordering on your phone, scanning a QR code, or perusing a digital kiosk, the humble menu is…

  • AI-driven smart menus simplify navigation and understanding for diners.
  • Natural language processing identifies ingredients, cooking methods, and allergens.
  • Personalized recommendation systems enhance dining experience based on user preferences.

Building an AI workout coach in SwiftUI: one-tap logging during the set, GPT-4o analysis after

Most people who track their training in a notes app lose the one thing that matters: set-by-set context. The note does not remember what you lifted last week, and generic gym trackers bury the screen…

  • Body Forge app offers minimal in-set information with a one-tap logging feature
  • GPT-4o analyzes user data after workout for personalized feedback
  • App uses SwiftUI, Core Data, HealthKit, and CloudKit for iOS 17+

More from Sunday 4 October →