Skip to content

Chat with an assistant

Authenticate requests with your API key.

POSThttps://gptunnel.ru/v1/assistant/chat

Send a message to an assistant and receive its reply in the context of the conversation.

Request headers

ParameterTypeDescription
Authorization*stringAPI key

Request body

ParameterTypeDescription
chatId*stringUnique chat ID generated by the client, 24–36 characters.
assistantCode*stringThe mention returned when you create the assistant.
message*stringMessage to the assistant
maxContextnumberNumber of previous messages to include in context, from 0 to 20. Defaults to 0. To continue the chat, send the same chatId and a positive value.
imagesArray<string>Array of image URLs for vision input.
streambooleantrue requests a stream response (experimentally). The default returns JSON.

Set GPTUNNEL_API_KEY to your key. Set GPTUNNEL_ASSISTANT_CODE to the mention from the assistant creation response. Generate a UUID for this conversation and store it in GPTUNNEL_CHAT_ID. The example sends the first message. For a follow-up such as “What word did I ask you to remember?”, reuse the same chatId and pass maxContext: 2.

import json
import os
from urllib.request import Request, urlopen
body = {
"assistantCode": os.environ['GPTUNNEL_ASSISTANT_CODE'],
"chatId": os.environ['GPTUNNEL_CHAT_ID'],
"message": "Запомни слово КЕДР. Ответь только этим словом.",
"maxContext": 0
}
request = Request(
'https://gptunnel.ru/v1/assistant/chat',
headers={
'Authorization': os.environ['GPTUNNEL_API_KEY'],
'Content-Type': 'application/json',
},
data=json.dumps(body).encode('utf-8'),
)
with urlopen(request, timeout=60) as response:
print(json.load(response))

usage covers the entire turn. If the assistant calls functions, MCP tools, or knowledge base search as a tool, the token counts and charges from all calls are added together. For a billed request, total_cost is the amount actually charged in RUB after your discount.

Knowledge base search in the every-request or first-message mode runs before the model call. It is billed separately and is excluded from usage. Search in function mode is included. When there is no charge, such as for a free model, the cost fields show an estimate. The prompt_cost and completion_cost fields are parts of the same charge, so token count multiplied by the list price may give a different amount.

Part of the response to the request to repeat “KEDR”.

{
"chatId": "a11e52c2-5ecc-4d86-a9fa-0cac92414a72",
"assistantId": "68c9b2d1e4b0a2d3c4f5e6b8",
"assistantCode": "example-assistant",
"message": "КЕДР",
"model": "gpt-6-luna",
"usage": {
"context_messages": 2,
"prompt_tokens": 1175,
"completion_tokens": 37,
"total_tokens": 1212,
"total_cost": 0.03116
}
}

With stream: true, the server returns Content-Type: text/event-stream. Each nonempty line contains a JSON object without a data: prefix. Read the stream line by line, skip blank lines, and append each delta to the response text. The final object contains finishReason.

For a response of “4”, the first object contained "delta": "", followed by an object with "delta": "4" and "finishReason": "stop". See the separate Chat Completions streaming format.