{
  "openapi": "3.1.0",
  "info": {
    "title": "TokenTool Public API",
    "version": "0.1.0",
    "description": "Минимальный публичный контракт TokenTool: каталог моделей и непотоковые текстовые запросы. Спецификация намеренно не заявляет другие API-поверхности."
  },
  "servers": [
    {
      "url": "https://api.tokentool.ru/api/v1",
      "description": "Публичный API TokenTool"
    }
  ],
  "tags": [
    {
      "name": "Chat",
      "description": "Непотоковые текстовые запросы"
    },
    {
      "name": "Models",
      "description": "Публичный каталог моделей и рублёвых цен"
    }
  ],
  "paths": {
    "/chat/completions": {
      "post": {
        "operationId": "createChatCompletion",
        "summary": "Создать текстовый ответ модели",
        "description": "Минимальный OpenAI-совместимый запрос с идентификатором модели и массивом сообщений. Потоковый режим и специальные функции SDK не входят в этот контракт.",
        "tags": [
          "Chat"
        ],
        "security": [
          {
            "BearerAuth": []
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ChatCompletionRequest"
              },
              "example": {
                "model": "provider/model-id",
                "messages": [
                  {
                    "role": "user",
                    "content": "Ответьте одним предложением: что такое API?"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Текстовый ответ модели",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ChatCompletionResponse"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ClientError"
          },
          "401": {
            "$ref": "#/components/responses/ClientError"
          },
          "default": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    },
    "/models": {
      "get": {
        "operationId": "listModels",
        "summary": "Получить актуальный каталог моделей",
        "description": "Публичный каталог TokenTool. Состав моделей, цены и время снимка меняются, поэтому клиенты не должны закреплять количество элементов.",
        "tags": [
          "Models"
        ],
        "security": [],
        "responses": {
          "200": {
            "description": "Актуальный снимок публичного каталога",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ModelCatalogResponse"
                }
              }
            }
          },
          "default": {
            "$ref": "#/components/responses/Error"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "TokenTool API key"
      }
    },
    "schemas": {
      "ChatMessage": {
        "type": "object",
        "required": [
          "role",
          "content"
        ],
        "properties": {
          "role": {
            "type": "string",
            "minLength": 1,
            "description": "Роль сообщения, например user."
          },
          "content": {
            "type": "string",
            "minLength": 1
          }
        },
        "additionalProperties": true
      },
      "ChatCompletionRequest": {
        "type": "object",
        "required": [
          "model",
          "messages"
        ],
        "properties": {
          "model": {
            "type": "string",
            "minLength": 1,
            "description": "Актуальный идентификатор модели из TokenTool."
          },
          "messages": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/components/schemas/ChatMessage"
            }
          }
        },
        "additionalProperties": true
      },
      "AssistantMessage": {
        "type": "object",
        "required": [
          "content"
        ],
        "properties": {
          "role": {
            "type": "string"
          },
          "content": {
            "type": "string"
          }
        },
        "additionalProperties": true
      },
      "ChatChoice": {
        "type": "object",
        "required": [
          "message"
        ],
        "properties": {
          "index": {
            "type": "integer",
            "minimum": 0
          },
          "message": {
            "$ref": "#/components/schemas/AssistantMessage"
          },
          "finish_reason": {
            "type": [
              "string",
              "null"
            ]
          }
        },
        "additionalProperties": true
      },
      "ChatCompletionResponse": {
        "type": "object",
        "required": [
          "choices"
        ],
        "properties": {
          "choices": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$ref": "#/components/schemas/ChatChoice"
            }
          }
        },
        "additionalProperties": true
      },
      "ContextPriceTier": {
        "type": "object",
        "required": [
          "min_prompt_tokens",
          "per_m_input",
          "per_m_output",
          "per_m_cache_read",
          "per_m_cache_write"
        ],
        "properties": {
          "min_prompt_tokens": {
            "type": "integer",
            "minimum": 0
          },
          "per_m_input": {
            "type": "number",
            "minimum": 0
          },
          "per_m_output": {
            "type": "number",
            "minimum": 0
          },
          "per_m_cache_read": {
            "type": "number",
            "minimum": 0
          },
          "per_m_cache_write": {
            "type": "number",
            "minimum": 0
          }
        },
        "additionalProperties": true
      },
      "ModelPricingRub": {
        "type": "object",
        "required": [
          "per_m_input",
          "per_m_output",
          "per_image",
          "per_audio",
          "per_video",
          "per_m_cache_read",
          "per_m_cache_write",
          "context_tiers"
        ],
        "properties": {
          "per_m_input": {
            "type": "number",
            "minimum": 0
          },
          "per_m_output": {
            "type": "number",
            "minimum": 0
          },
          "per_image": {
            "type": "number",
            "minimum": 0
          },
          "per_audio": {
            "type": "number",
            "minimum": 0
          },
          "per_video": {
            "type": "number",
            "minimum": 0
          },
          "per_m_cache_read": {
            "type": "number",
            "minimum": 0
          },
          "per_m_cache_write": {
            "type": "number",
            "minimum": 0
          },
          "context_tiers": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ContextPriceTier"
            }
          }
        },
        "additionalProperties": true
      },
      "ModelPricingUsd": {
        "type": "object",
        "required": [
          "per_m_input",
          "per_m_output",
          "per_image",
          "per_audio",
          "per_video"
        ],
        "properties": {
          "per_m_input": {
            "type": "number",
            "minimum": 0
          },
          "per_m_output": {
            "type": "number",
            "minimum": 0
          },
          "per_image": {
            "type": "number",
            "minimum": 0
          },
          "per_audio": {
            "type": "number",
            "minimum": 0
          },
          "per_video": {
            "type": "number",
            "minimum": 0
          }
        },
        "additionalProperties": true
      },
      "ModelCatalogItem": {
        "type": "object",
        "required": [
          "id",
          "name",
          "description",
          "type",
          "category",
          "provider",
          "api_provider",
          "provider_ok",
          "context_length",
          "capabilities",
          "tags",
          "pricing_usd",
          "pricing_rub"
        ],
        "properties": {
          "id": {
            "type": "string",
            "minLength": 1
          },
          "name": {
            "type": "string",
            "minLength": 1
          },
          "description": {
            "type": "string"
          },
          "type": {
            "type": "string"
          },
          "category": {
            "type": "string"
          },
          "provider": {
            "type": "string"
          },
          "api_provider": {
            "type": "string"
          },
          "provider_ok": {
            "type": "boolean"
          },
          "context_length": {
            "type": "integer",
            "minimum": 1
          },
          "capabilities": {
            "type": "array",
            "description": "Массив метаданных; контракт элементов этой версии не закреплён.",
            "items": {}
          },
          "tags": {
            "type": "array",
            "description": "Массив метаданных; контракт элементов этой версии не закреплён.",
            "items": {}
          },
          "pricing_usd": {
            "$ref": "#/components/schemas/ModelPricingUsd"
          },
          "pricing_rub": {
            "$ref": "#/components/schemas/ModelPricingRub"
          }
        },
        "additionalProperties": true
      },
      "ModelCatalogResponse": {
        "type": "object",
        "required": [
          "data",
          "cached",
          "fetched_at"
        ],
        "properties": {
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/ModelCatalogItem"
            }
          },
          "cached": {
            "type": "boolean"
          },
          "fetched_at": {
            "type": "string",
            "format": "date-time"
          }
        },
        "additionalProperties": true
      },
      "ErrorResponse": {
        "type": "object",
        "description": "Форма ошибки может зависеть от класса отказа; клиенту следует опираться на HTTP status.",
        "additionalProperties": true
      }
    },
    "responses": {
      "ClientError": {
        "description": "Запрос или авторизация отклонены",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      },
      "Error": {
        "description": "Ошибка API",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/ErrorResponse"
            }
          }
        }
      }
    }
  }
}
