TsgcHTTP_API_AnthropicMethods › CountTokens

CountTokens Method

Bir isteği Claude'a göndermeden önce tüketeceği giriş token'larının sayısını sayar

Sözdizimi

function CountTokens(const aRequest: TsgcAnthropicClass_Request_CountTokens) : TsgcAnthropicClass_Response_CountTokens;

Parametreler

NameTypeAçıklama
aRequestconst TsgcAnthropicClass_Request_CountTokensCreateMessage'a göndereceğiniz aynı Model, System ve Messages alanlarına sahip istek nesnesi.

Dönüş Değeri

İsteğin kullanacağı InputTokens sayısını içeren yanıt (TsgcAnthropicClass_Response_CountTokens)

Remarks

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.

Örnek

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;

Yöntemlere Dön