# Токенизация

В отличие от веб-интерфейса, где вся токенизация происходит на BPE 100k, написанном на C++ ([**wasm-tokenizer**](https://github.com/script-heads/wasm-tokenizer)), в API токены считает провайдер LLM (например, OpenAI для GPT, Anthropic для Claude и т.д.).

Если вы хотите предварительно посчитать токены перед отправкой, вам необходимо использовать библиотеки для подсчета, такие как [tiktoken](https://github.com/openai/tiktoken) и другие, в зависимости от модели, которую вы будете использовать.

При отправке запроса в наш API в ответ вы получите полный отчет об использовании токенов.&#x20;

Например, при отправке запроса в Assistant API в ответе будет объект `usage`, где можно увидеть, сколько токенов было потрачено и на какие цели:

```json
{
	"id": "66c6dcf2e2826d0001f48d37",
	"chatId": "a11e52c2-5ecc-4d86-a9fa-0cac92414a72",
	"assistantId": "637eedd95a56c03462f9c231",
	"assistantCode": "test_gpt4o_assistant",
	"message": "Hello! How can I assist you today?",
	"model": "gpt-4o",
	"usage": {
		"context_messages": 2,
		"prompt_tokens": 111,
		"completion_tokens": 10,
		"embedding_tokens": 0,
		"total_tokens": 121,
		"prompt_cost": 0.14985,
		"completion_cost": 0.027,
		"embedding_cost": 0,
		"total_cost": 0.17685
	}
}
```

Для прямых [запросов в OpenAI](https://docs.gptunnel.ru/api-chatgpt/chat-completions-1) ответ будет немного отличаться, но тут мы также можем видеть объект `usage` со статистикой использования токенов:

```json
{
  "id": "chatcmpl-8FheRf68Hi4pnuiRqYPHyZJr3UlAU",
  "object": "chat.completion",
  "created": 1698753407,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
      	"role": "assistant",
        "content": "Меня зовут Robert."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 24,
    "completion_tokens": 8,
    "total_tokens": 32,
    "prompt_cost": 0.01608,
    "completion_cost": 0.0072,
    "total_cost": 0.02328
    }
}
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gptunnel.ru/tokenizer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
