Your first customer cost breakdown

Send three short requests for two example customers, then see which customer and feature generated the cost. Use synthetic data to verify the setup before connecting your application.

1. Prepare your project

  1. Create an account and finish workspace setup.
  2. Open Projects and create a full Spanlens key for your project. Save the key when it is shown.
  3. Add an OpenAI provider key under that same Spanlens key.

Use Node.js 20.6 or later. Download the customer-cost.mjs exampleinto a new local folder. It uses Node's built-in fetch and needs no package installation.

2. Preview the calls

bash
node customer-cost.mjs --dry-run

The preview sends no requests and needs no credentials.

CustomerFeatureRequests
example-customer-asupport-reply1
example-customer-asummarize1
example-customer-bsupport-reply1

3. Send your first requests

Create a local .env file beside the script. Keep this file out of version control.

env
SPANLENS_API_KEY=sl_live_your_full_project_key
# Optional: choose another chat-completions model available to your OpenAI account.
SPANLENS_EXAMPLE_MODEL=gpt-4o-mini
bash
node --env-file=.env customer-cost.mjs

This sends three real OpenAI requests; standard provider charges apply. Each response is capped at 64 output tokens. The script uses synthetic customer IDs and metadata-only logging, so prompt and response bodies are not stored by Spanlens.

4. Verify the request rows

Open Requests in the same workspace and project. After logging completes, look for the three calls and check their model, tokens, cost, User, and Session. Refresh if the rows have not arrived yet.

  • HTTP 401/403: check that the Spanlens key is active and full-scope.
  • Provider-key error: register the OpenAI key under the Spanlens key used by this script.
  • Model-access error: choose a model your OpenAI account can call.
  • HTTP 429: check both provider limits and Spanlens quota.
  • A call completed but no row appeared: check project and date filters, then follow the quick-start verification steps.

For a timeout or partial failure, check the request log before running again. The example does not automatically retry calls.

5. Find the customer and feature behind the cost

  1. Open Users and find the two example customers. Compare their request counts, tokens, and total cost over the same date range.
  2. Open customer A's requests. Inspect Session to distinguish support-reply from summarize.
  3. Open a request to inspect its token and cost breakdown. Export the filtered requests when you need a report.

Customer A has two calls and customer B has one. Actual costs depend on token usage and the selected model. This example demonstrates attribution; it does not represent customer adoption or measured savings.

6. Apply the same tags to your application

Use a stable internal customer ID and a session ID that identifies a feature run.

ts
import { createOpenAI, withUser, withSession, withLogBody } from '@spanlens/sdk/openai'

const openai = createOpenAI()
await openai.chat.completions.create(
  {
    model: 'gpt-4o-mini',
    max_tokens: 64,
    messages: [{ role: 'user', content: 'Write a short support reply.' }],
  },
  {
    headers: {
      ...withUser('your-internal-customer-id').headers,
      ...withSession('support-reply:your-run-id').headers,
      ...withLogBody('meta').headers,
    },
  },
)

Install @spanlens/sdk and openai in your application. Start with one feature, review its actual costs, and decide what to investigate next. See the customer analytics guide for the dashboard details.