MeterTrail
← All tutorials

Track your first AI request with Python

Use the Python standard library to send a trace and verify your first successful integration.

Before you start

Python 3.10 or later, a MeterTrail account and a project API key.

Keep project API keys out of browser code, screenshots and source control. No model API key is required for this test.

1. Create a project and API key

Create a free account, verify your email, then create or select a project in Project management. Open API keys for that project, create a key and complete email verification. Save the key when it is displayed.

Create a free account

2. Set your environment

# macOS / Linux / WSL
export METERTRAIL_ENDPOINT="https://metertrail.com"
printf 'Project API key (input is visible): '
IFS= read -r METERTRAIL_API_KEY
export METERTRAIL_API_KEY

# Windows PowerShell (Node.js / Python tutorials)
$env:METERTRAIL_ENDPOINT = "https://metertrail.com"
$env:METERTRAIL_API_KEY = Read-Host "Project API key" -MaskInput

3. Send a sample trace

Save as send.py and run from the same terminal where you set the environment variables.

import json
import os
import urllib.request
import uuid
from datetime import datetime, timezone

batch = {
    'schemaVersion': '1.0',
    'agent': {
        'serviceName': 'python-example',
        'environment': 'development',
        'instanceId': str(uuid.uuid4()),
    },
    'events': [
        {
            'eventId': str(uuid.uuid4()),
            'type': 'trace',
            'objectId': str(uuid.uuid4()),
            'revision': 1,
            'traceId': uuid.uuid4().hex,
            'startedAt': datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z'),
            'data': {
                'name': 'Example request',
                'status': 'success',
                'durationMs': 120,
            },
        }
    ],
}
request = urllib.request.Request(
    os.environ['METERTRAIL_ENDPOINT'] + '/v1/events/batch',
    data=json.dumps(batch).encode(),
    headers={
        'Authorization': 'Bearer ' + os.environ['METERTRAIL_API_KEY'],
        'Content-Type': 'application/json',
    },
)
with urllib.request.urlopen(request, timeout=10) as response:
    print(response.status, response.read().decode())
python3 send.py

Use this server-side example after your application finishes a request. Read duration from your timer and status from the actual result. The example uses no third-party packages and does not send prompt or response text.

4. Check the result

A successful upload returns HTTP 200. pending is a receipt for asynchronous processing, not a confirmation that data is already queryable.

{"batchId":"<batch-id>","received":1,"status":"pending"}

Optionally query the returned batchId using the same project key:

curl --fail-with-body --max-time 10 "https://metertrail.com/v1/batches/<batch-id>" \
  -H "Authorization: Bearer $METERTRAIL_API_KEY"

5. Find the trace in your console

Select the project associated with your key and open Traces. Refresh after processing and find Example request. Model cost can be Unknown because this minimal example does not report model usage or prices.

Troubleshooting

401: check the key and project. 400: check required fields and timestamps. For quota or subscription errors, check Billing. After a network timeout retry the same event IDs and content rather than creating new objects.

Full ingestion field reference

Other tutorials:

Track your first AI request with Node.js · Send your first trace with REST and cURL