> For the complete documentation index, see [llms.txt](https://docs.snapfix.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.snapfix.com/engineering-reference/db/tables/ai_token_usage.md).

# ai\_token\_usage

{% hint style="info" %}
Generated from the staging database schema, with descriptions drafted by agents ([how to read these docs](https://docs.snapfix.com/how-to-read.md)). [Source on GitHub](https://github.com/snapfix/docs/blob/main/spec/db/tables/ai_token_usage.md).
{% endhint %}

## Description

One AI call (translation, tag, room, area, title, group, assignee or Simon) with its provider, model, tokens, cost and outcome. api\_php\_live\_v2 writes the translation calls and aitask\_pyserver the others; rows keep request and answer text with no retention.

<details>

<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `ai_token_usage` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `uuid_tParent` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `uuid_tUser` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `uuid_tBusiness` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'groupUUID from ai-task routes',
  `snapfixSource` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'X-Snapfix-Source / snapfixSource when present',
  `feature` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `provider` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `model` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `inputWordCount` int DEFAULT '0' COMMENT 'specific to AI translation service',
  `inputCharacterCount` int DEFAULT '0' COMMENT 'specific to AI translation service',
  `promptTokens` int DEFAULT '0',
  `completionTokens` int DEFAULT '0',
  `totalTokens` int DEFAULT '0',
  `estimatedCost` decimal(12,6) DEFAULT '0.000000',
  `status` enum('success','failed') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT 'success',
  `sessionId` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `commandType` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `domain` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `outcome` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `durationMs` int DEFAULT NULL,
  `llmRounds` smallint DEFAULT NULL,
  `commandText` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `responseText` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `errorCode` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `turnLogS3Key` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `fCreatedTs` bigint DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_parent_ts` (`uuid_tParent`,`fCreatedTs`) USING BTREE,
  KEY `idx_user_ts` (`uuid_tUser`,`fCreatedTs`) USING BTREE,
  KEY `idx_ai_token_usage_user_created` (`uuid_tUser`,`fCreatedTs`) USING BTREE,
  KEY `idx_ai_token_usage_parent_created` (`uuid_tParent`,`fCreatedTs`) USING BTREE,
  KEY `idx_ai_token_usage_command_type` (`commandType`,`fCreatedTs`) USING BTREE,
  KEY `idx_ai_token_usage_business_created` (`uuid_tBusiness`,`fCreatedTs`),
  KEY `idx_ai_token_usage_domain_created` (`domain`,`fCreatedTs`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC
```

</details>

## Columns

| Name                | Type                     | Default  | Nullable | Extra Definition | Comment                                                                                                           |
| ------------------- | ------------------------ | -------- | -------- | ---------------- | ----------------------------------------------------------------------------------------------------------------- |
| id                  | bigint                   |          | false    | auto\_increment  |                                                                                                                   |
| uuid\_tParent       | varchar(50)              |          | true     |                  |                                                                                                                   |
| uuid\_tUser         | varchar(50)              |          | true     |                  |                                                                                                                   |
| uuid\_tBusiness     | varchar(50)              |          | true     |                  | groupUUID from ai-task routes                                                                                     |
| snapfixSource       | varchar(64)              |          | true     |                  | X-Snapfix-Source / snapfixSource when present                                                                     |
| feature             | varchar(50)              |          | true     |                  | translation, tag, room, area, title, group, assignee or simon.                                                    |
| provider            | varchar(50)              |          | true     |                  |                                                                                                                   |
| model               | varchar(100)             |          | true     |                  |                                                                                                                   |
| inputWordCount      | int                      | 0        | true     |                  | specific to AI translation service                                                                                |
| inputCharacterCount | int                      | 0        | true     |                  | specific to AI translation service                                                                                |
| promptTokens        | int                      | 0        | true     |                  |                                                                                                                   |
| completionTokens    | int                      | 0        | true     |                  |                                                                                                                   |
| totalTokens         | int                      | 0        | true     |                  |                                                                                                                   |
| estimatedCost       | decimal(12,6)            | 0.000000 | true     |                  |                                                                                                                   |
| status              | enum('success','failed') | success  | true     |                  | success or failed.                                                                                                |
| sessionId           | varchar(64)              |          | true     |                  |                                                                                                                   |
| commandType         | varchar(64)              |          | true     |                  |                                                                                                                   |
| domain              | varchar(64)              |          | true     |                  |                                                                                                                   |
| outcome             | varchar(32)              |          | true     |                  | Result of the call, for example success, ok, empty, clarification, timeout, unknown, failed or bulk\_task\_batch. |
| durationMs          | int                      |          | true     |                  |                                                                                                                   |
| llmRounds           | smallint                 |          | true     |                  |                                                                                                                   |
| commandText         | varchar(500)             |          | true     |                  |                                                                                                                   |
| responseText        | varchar(1000)            |          | true     |                  |                                                                                                                   |
| errorCode           | varchar(64)              |          | true     |                  |                                                                                                                   |
| turnLogS3Key        | varchar(512)             |          | true     |                  |                                                                                                                   |
| fCreatedTs          | bigint                   |          | true     |                  |                                                                                                                   |

## Viewpoints

| Name                                                                           | Definition                                                                                                                                             |
| ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| [ai](https://docs.snapfix.com/engineering-reference/db/tables/viewpoint-17.md) | Snapfix AI and AI suggestions: agent sessions and messages, brand documents, task insight and title-style data, token usage, the AI job log and queue. |

## Constraints

| Name    | Type        | Definition       |
| ------- | ----------- | ---------------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name                                     | Definition                                                                             |
| ---------------------------------------- | -------------------------------------------------------------------------------------- |
| idx\_ai\_token\_usage\_business\_created | KEY idx\_ai\_token\_usage\_business\_created (uuid\_tBusiness, fCreatedTs) USING BTREE |
| idx\_ai\_token\_usage\_command\_type     | KEY idx\_ai\_token\_usage\_command\_type (commandType, fCreatedTs) USING BTREE         |
| idx\_ai\_token\_usage\_domain\_created   | KEY idx\_ai\_token\_usage\_domain\_created (domain, fCreatedTs) USING BTREE            |
| idx\_ai\_token\_usage\_parent\_created   | KEY idx\_ai\_token\_usage\_parent\_created (uuid\_tParent, fCreatedTs) USING BTREE     |
| idx\_ai\_token\_usage\_user\_created     | KEY idx\_ai\_token\_usage\_user\_created (uuid\_tUser, fCreatedTs) USING BTREE         |
| idx\_parent\_ts                          | KEY idx\_parent\_ts (uuid\_tParent, fCreatedTs) USING BTREE                            |
| idx\_user\_ts                            | KEY idx\_user\_ts (uuid\_tUser, fCreatedTs) USING BTREE                                |
| PRIMARY                                  | PRIMARY KEY (id) USING BTREE                                                           |

## Relations

```mermaid
erDiagram


"ai_token_usage" {
  bigint id PK
}
```

***

> Generated by [tbls](https://github.com/k1LoW/tbls)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.snapfix.com/engineering-reference/db/tables/ai_token_usage.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
