# Query AI metrics

`POST /metrics/ai`



[Observability API guide](https://docs.contextual.io/legacy/tenants/tenant-apis/observability-api.md)

```json
{
  "openapi": "3.0.3",
  "info": {
    "title": "Observability API",
    "description": "Query logs, metrics, traces, and solution analytics against ClickHouse. See **Solution Metrics** endpoints and external docs for the AI reference guide.",
    "version": "1.7.7"
  },
  "security": [
    {
      "jwt": []
    }
  ],
  "servers": [
    {
      "url": "https://observability.{tenantId}.my.contextual.io/api/v1",
      "variables": {
        "tenantId": {
          "default": "YOUR-TENANT-ID"
        }
      }
    }
  ],
  "paths": {
    "/metrics/ai": {
      "post": {
        "tags": [
          "AI Metrics"
        ],
        "summary": "Query AI metrics",
        "parameters": [],
        "deprecated": false,
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "filter": {
                    "type": "object",
                    "properties": {
                      "service": {
                        "type": "string"
                      },
                      "metricName": {
                        "type": "string",
                        "enum": [
                          "request-count",
                          "input-tokens",
                          "output-tokens",
                          "cache-read-tokens",
                          "cache-write-tokens",
                          "reasoning-tokens",
                          "cache-hit-ratio",
                          "latency",
                          "error-rate",
                          "ttft",
                          "input-cost",
                          "output-cost",
                          "total-cost",
                          "uncached-input-cost",
                          "cache-read-cost",
                          "cache-write-cost",
                          "reasoning-cost"
                        ]
                      },
                      "agentId": {
                        "type": "string"
                      },
                      "provider": {
                        "type": "string"
                      },
                      "modelId": {
                        "type": "string"
                      },
                      "routeId": {
                        "type": "string"
                      },
                      "nodeId": {
                        "type": "string"
                      },
                      "status": {
                        "type": "string",
                        "enum": [
                          "error",
                          "success"
                        ]
                      },
                      "finishReason": {
                        "type": "string"
                      }
                    },
                    "required": [
                      "service"
                    ],
                    "additionalProperties": false
                  },
                  "last": {
                    "type": "string",
                    "pattern": "^-?\\d+[dhm]$"
                  },
                  "from": {
                    "type": "string",
                    "pattern": "^(?:\\d+(?:\\.\\d+?)?|(?:\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?Z))$",
                    "description": "An ISO 8601 date time string",
                    "format": "date-time"
                  },
                  "to": {
                    "type": "string",
                    "pattern": "^(?:\\d+(?:\\.\\d+?)?|(?:\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?Z))$",
                    "description": "An ISO 8601 date time string",
                    "format": "date-time"
                  },
                  "rollup": {
                    "type": "object",
                    "properties": {
                      "bucket": {
                        "default": "1 MINUTE",
                        "type": "string",
                        "enum": [
                          "1 MINUTE",
                          "5 MINUTE",
                          "15 MINUTE",
                          "30 MINUTE",
                          "1 HOUR",
                          "6 HOUR",
                          "1 DAY"
                        ]
                      }
                    },
                    "required": [
                      "bucket"
                    ],
                    "additionalProperties": false
                  },
                  "groupBy": {
                    "type": "string",
                    "enum": [
                      "provider-model",
                      "route",
                      "agent",
                      "node",
                      "finish-reason"
                    ]
                  },
                  "limit": {
                    "default": 100,
                    "type": "integer",
                    "exclusiveMinimum": 0,
                    "maximum": 10000
                  },
                  "offset": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 1000000
                  },
                  "output": {
                    "default": "table",
                    "type": "string",
                    "enum": [
                      "table",
                      "timeseries",
                      "summary",
                      "group",
                      "tool-calls-table",
                      "tool-calls-timeseries",
                      "fallback-chains",
                      "fallback-table",
                      "route-health"
                    ]
                  }
                },
                "required": [
                  "filter",
                  "limit",
                  "output"
                ],
                "additionalProperties": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "propertyNames": {
                    "type": "string"
                  },
                  "additionalProperties": {}
                }
              }
            }
          },
          "400": {
            "description": "Invalid request",
            "content": {}
          },
          "401": {
            "description": "Missing or invalid authorization header",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/$ErrorResponse"
                }
              }
            }
          },
          "403": {
            "description": "Invalid permissions",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/$ErrorResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "jwt": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "schemas": {
      "$ErrorResponse": {
        "type": "object",
        "properties": {
          "code": {
            "type": [
              "string",
              "number"
            ]
          },
          "message": {
            "type": "string"
          },
          "errors": {
            "type": "array",
            "items": {}
          }
        },
        "required": [
          "code",
          "message"
        ],
        "additionalProperties": false
      }
    }
  }
}
```