> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trainlyai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Analytics API

> Query metrics, costs, performance stats, and export trace logs.

## Metrics Summary

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://api.trainlyai.com/v1/{project_id}/analytics/metrics" \
    -H "Authorization: Bearer tk_your_api_key"
  ```
</CodeGroup>

`GET /v1/{project_id}/analytics/metrics`

### Response

```json theme={null}
{
  "total_queries": 48210,
  "successful": 47895,
  "failed": 315,
  "avg_response_time_ms": 723.4,
  "total_cost": 142.87,
  "avg_cost_per_query": 0.00296
}
```

***

## Export Logs

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://api.trainlyai.com/v1/{project_id}/analytics/export?format=csv" \
    -H "Authorization: Bearer tk_your_api_key" \
    -o traces.csv
  ```
</CodeGroup>

`GET /v1/{project_id}/analytics/export`

### Query Parameters

| Parameter | Type   | Default | Description                              |
| --------- | ------ | ------- | ---------------------------------------- |
| `format`  | string | `json`  | Export format: `csv`, `json`, or `jsonl` |

Returns the file directly with the appropriate `Content-Type` header.

***

## Cost Breakdown

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://api.trainlyai.com/v1/{project_id}/analytics/costs" \
    -H "Authorization: Bearer tk_your_api_key"
  ```
</CodeGroup>

`GET /v1/{project_id}/analytics/costs`

### Response

```json theme={null}
{
  "total_cost": 142.87,
  "llm_cost": 128.53,
  "embedding_cost": 11.20,
  "reranking_cost": 3.14,
  "currency": "USD",
  "period_start": "2026-04-01T00:00:00Z",
  "period_end": "2026-04-07T23:59:59Z"
}
```

***

## Performance Stats

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://api.trainlyai.com/v1/{project_id}/analytics/performance" \
    -H "Authorization: Bearer tk_your_api_key"
  ```
</CodeGroup>

`GET /v1/{project_id}/analytics/performance`

### Response

```json theme={null}
{
  "p50_latency_ms": 540.2,
  "p95_latency_ms": 1823.7,
  "p99_latency_ms": 3102.1,
  "avg_latency_ms": 723.4,
  "total_traces": 48210,
  "period_start": "2026-04-01T00:00:00Z",
  "period_end": "2026-04-07T23:59:59Z"
}
```
