TsgcHTTP_API_Anthropic › Methods › CountTokens
Bir isteği Claude'a göndermeden önce tüketeceği giriş token'larının sayısını sayar
function CountTokens(const aRequest: TsgcAnthropicClass_Request_CountTokens) : TsgcAnthropicClass_Response_CountTokens;
| Name | Type | Açıklama |
|---|---|---|
aRequest | const TsgcAnthropicClass_Request_CountTokens | CreateMessage'a göndereceğiniz aynı Model, System ve Messages alanlarına sahip istek nesnesi. |
İsteğin kullanacağı InputTokens sayısını içeren yanıt (TsgcAnthropicClass_Response_CountTokens)
Bir komut isteminin (prompt) aslında bir tamamlama üretmeden ne kadar girdi token'ı tüketeceğini hesaplamak için Anthropic API'sine bir POST /v1/messages/count_tokens çağrısı gönderir. Bu, maliyet tahmini, istek ön doğrulaması ve hedef modelin bağlam penceresinin altında kalmak için kullanışlıdır. AnthropicOptions içindeki anthropic-version başlığı otomatik olarak dahil edilir. Döndürülen nesne çağıran tarafından serbest bırakılmalıdır.
oRequest := TsgcAnthropicClass_Request_CountTokens.Create;
oRequest.Model := 'claude-sonnet-4-5';
oRequest.Messages.Add('user', 'How many tokens is this sentence?');
oResponse := oAPI.CountTokens(oRequest);
try ShowMessage(IntToStr(oResponse.InputTokens)); finally oResponse.Free; end;