iconLensAI

Cost Attribution

How LensAI tracks, calculates, and attributes AI costs to customers, agents, and business dimensions

Cost Attribution

Cost attribution is the foundation of AI business intelligence. LensAI automatically tracks what you spend on LLM calls, API usage, and compute resources, then attributes these costs to the customers, agents, sessions, and business dimensions that matter to you. This visibility enables you to answer critical questions like "What does it cost to serve this customer?" and "Which agents are profitable?"

How Cost Tracking Works

LensAI captures costs in real-time as requests flow through the proxy layer. For each LLM call, LensAI:

  1. Measures token usage: Extracts prompt tokens and completion tokens from the provider response
  2. Applies current pricing: Uses up-to-date pricing tables for each model and provider
  3. Calculates cost: Multiplies tokens by the model's per-token rate
  4. Enriches with context: Associates the cost with customer IDs, agent names, session IDs, and other metadata from your request
  5. Stores the event: Persists the cost data alongside operational metrics for analysis

This happens automatically for all supported providers (OpenAI, Anthropic, Google, etc.) without any manual configuration. Costs are calculated down to the fraction of a cent, ensuring accurate tracking even for high-volume, low-cost interactions.

Automatic Attribution Dimensions

Out of the box, LensAI attributes costs to several dimensions:

By Model Every cost is tagged with the specific model used (e.g., gpt-4, gpt-3.5-turbo, claude-3-opus). This allows you to see spending breakdowns by model and identify opportunities to route requests to cheaper alternatives.

By Provider If you use multiple providers, LensAI tracks spending per provider, helping you understand dependencies and negotiate better rates.

By Time All costs are timestamped, enabling time-series analysis. Track daily, weekly, or monthly spending, identify trends, and forecast future costs.

By Project If you have multiple projects (production, staging, development), costs are automatically scoped to the project that made the request.

Custom Attribution with Metadata

The real power of cost attribution comes from adding business context to your requests. By including metadata in your LLM calls, you can attribute costs to any dimension relevant to your business.

Attributing to Customers

Include a customer_id in your request metadata:

const response = await client.chat.completions.create({
  model: 'gpt-4',
  messages: [...],
  metadata: {
    customer_id: 'user_123'
  }
});

Now LensAI can aggregate all costs associated with user_123, showing you exactly what it costs to serve that customer. This is essential for:

  • Calculating customer-level profitability (cost vs revenue from that customer)
  • Identifying high-cost customers who may need usage limits or pricing adjustments
  • Segmenting customers by cost to understand which customer types are sustainable

Attributing to Agents

Tag requests with the agent or feature that initiated them:

metadata: {
  agent: 'customer_support',
  feature: 'chat_assistant'
}

This enables you to:

  • Compare costs across agents to identify which workflows are expensive
  • Measure ROI per agent by comparing costs to value delivered or goals achieved
  • Optimize agent design by understanding which parts of multi-agent systems drive costs

Attributing to Sessions

Group related requests into sessions:

metadata: {
  session_id: 'session_abc123',
  customer_id: 'user_123'
}

Session-level attribution lets you:

  • Calculate cost per conversation or cost per task completed
  • Understand the full cost of customer interactions, not just individual API calls
  • Identify expensive sessions that may indicate inefficient workflows or edge cases

Custom Dimensions

You can add any metadata fields relevant to your business:

metadata: {
  customer_id: 'user_123',
  agent: 'onboarding_assistant',
  session_id: 'session_456',
  plan_tier: 'enterprise',
  region: 'us-west',
  feature_flag: 'advanced_reasoning_enabled',
  department: 'sales'
}

LensAI makes all of these dimensions available for filtering, segmentation, and aggregation in dashboards and reports.

Cost Aggregation and Rollups

LensAI pre-computes cost aggregations to make querying fast and efficient:

Per-request costs: The cost of each individual LLM call Per-session costs: The sum of all request costs within a session Per-customer costs: The sum of all costs attributed to a customer over a time period Per-agent costs: The sum of all costs for a specific agent or feature

You can query these aggregations at any level, filter by date ranges, and drill down from high-level summaries to individual requests.

Handling Complex Cost Scenarios

Multi-Model Workflows

If a single customer interaction involves multiple LLM calls using different models, LensAI accurately tracks the total cost:

User query → GPT-4 call ($0.03) + GPT-3.5 call ($0.002) + Embedding call ($0.0001)
Total session cost: $0.0321

All of these costs are rolled up to the session and customer level automatically.

Tool and API Costs

In addition to LLM costs, you may want to track costs for external tools and APIs called by your agents. You can manually record these costs:

await client.costs.record({
  session_id: 'session_456',
  amount: 0.05,
  description: 'Stripe API call',
  cost_type: 'api_usage'
});

LensAI will include these costs in session and customer aggregations alongside LLM costs.

Compute and Infrastructure Costs

For agentic systems that involve significant compute (e.g., running models locally, executing code, or processing large datasets), you can record infrastructure costs:

await client.costs.record({
  session_id: 'session_456',
  amount: 0.10,
  description: 'EC2 instance for code execution',
  cost_type: 'compute'
});

This ensures your cost attribution reflects the full operational expense of running your AI systems.

Cost Allocation Strategies

Different businesses have different needs for cost allocation. LensAI supports several strategies:

Direct Attribution

The simplest approach: every cost is directly attributed to the customer, agent, or session that triggered it. This works well for single-tenant systems or clear per-customer usage.

Shared Cost Allocation

For costs that benefit multiple customers (e.g., batch processing, model fine-tuning, infrastructure overhead), you can allocate costs proportionally:

  • By usage: Split shared costs based on each customer's share of total requests or tokens
  • By revenue: Allocate costs in proportion to revenue from each customer
  • Evenly: Divide shared costs equally among all active customers

LensAI allows you to tag certain costs as "shared" and configure allocation rules in your project settings.

Tiered Allocation

For subscription-based pricing, you might allocate costs differently based on customer plan tier:

  • Free tier: Track costs but don't allocate to specific customers (aggregate to a "free_users" cost center)
  • Paid tiers: Attribute costs directly to individual customers

Using Cost Attribution for Decision-Making

Once you have granular cost attribution, you can make data-driven decisions across your business:

Product Development

  • Feature prioritization: Invest in optimizing high-cost features that deliver high value
  • Model selection: Route simple queries to cheap models, complex queries to premium models
  • Agent design: Refactor agents with poor cost-to-value ratios

Pricing Strategy

  • Usage-based pricing: Charge customers based on actual costs with a margin
  • Tiered pricing: Set tier limits based on cost thresholds
  • Overage charges: Bill for usage that exceeds plan allowances

Customer Management

  • Identify unprofitable customers: Reach out to optimize usage or adjust pricing
  • Reward efficient users: Recognize customers who use AI features effectively without excessive costs
  • Predict churn: High costs relative to engagement may indicate poor product-market fit

Financial Planning

  • Budget forecasting: Project future costs based on growth rates and usage trends
  • Variance analysis: Compare actual costs to budgets and investigate deviations
  • Cost optimization tracking: Measure the impact of optimization initiatives over time

Best Practices for Cost Attribution

Attribute early and consistently. Add customer IDs, agent names, and session IDs to your metadata from day one. Retroactively adding attribution is difficult.

Use hierarchical IDs. Structure your identifiers to enable rollups (e.g., customer_id: "org_123:user_456" allows aggregation by organization and by individual user).

Tag experiments and A/B tests. Include feature flags or experiment IDs in metadata so you can isolate the cost impact of new features or changes.

Monitor for anomalies. Set up alerts for unusual cost spikes at the customer, agent, or project level. Investigate immediately to catch bugs, abuse, or unexpected usage patterns.

Reconcile with provider bills. Periodically compare LensAI's cost calculations against your actual provider invoices to ensure accuracy. Small discrepancies can occur due to pricing changes or special provider discounts.

Document your attribution schema. Maintain a clear mapping of what each metadata field represents and how it should be used, especially in teams where multiple developers instrument LLM calls.

Next Steps

Now that you understand cost attribution, you're ready to explore:

  • Value Modeling: Measure the tangible benefits your AI delivers to users
  • Revenue Mapping: Connect customer payments to AI usage for margin analysis
  • Segmentation: Slice and analyze costs across multiple business dimensions simultaneously