Подсчёт токенов
Для подсчёта токенов без генерации отправьте запрос на POST /api/v1/responses/input_tokens. Поля input и instructions разбираются как у /responses. Поле model можно передать, но на подсчёт оно не влияет.
В SDK OpenAI это client.responses.input_tokens.count(). Подсчёт токенов генерацию не запускает и лимит задачи не расходует.
Пример запроса
SDK OpenAI
Python
cURL
PHP
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://ai.mitup.ru/api/v1",
)
result = client.responses.input_tokens.count(
model="deepseek-flash",
input="Привет",
)
print(result)import requests
url = "https://ai.mitup.ru/api/v1/responses/input_tokens"
api_key = "YOUR_API_KEY"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
params = {
"model": "deepseek-flash",
"input": "Привет"
}
response = requests.post(url, json=params, headers=headers)
print(response.json())curl -X POST "https://ai.mitup.ru/api/v1/responses/input_tokens" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "deepseek-flash",
"input": "Привет"
}'<?php
$url = "https://ai.mitup.ru/api/v1/responses/input_tokens";
$api_key = "YOUR_API_KEY";
$headers = [
"Content-Type: application/json",
"Authorization: Bearer " . $api_key,
];
$params = [
"model" => "deepseek-flash",
"input" => "Привет"
];
$data = json_encode($params, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mitupai-API-client");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response, true);
print_r($response);
?>Пример возвращаемого результата:
Успех (200)
Ошибка (4хх)
{
"object": "response.input_tokens",
"input_tokens": 42
}{
"error": {
"message": "The input is too long to tokenize. Shorten the text or attach it as a file.",
"type": "invalid_request_error",
"param": "input",
"code": "invalid_request"
}
}