Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Logprobs

Get the log probability of each token the model generates.

Log probabilities (logprobs) show how likely the model considered each token that it generated. Use them to measure how confident the model is in an answer, to score classification labels or to find the parts of a response where the model was unsure.

Most chat completion models support logprobs. Embedding and audio models do not.


Request Parameters

Parameter
Type
Default
Description

logprobs

boolean

false

Return the log probability of each output token

top_logprobs

integer

-

Also return the most likely tokens at each position (0 to 20). Requires logprobs: true


Basic Usage

The examples on this page use deepseek/deepseek-v4.1-flash-1017. Replace it with any model that supports logprobs.

import math
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.tensorx.ai/v1"
)

response = client.chat.completions.create(
    model="deepseek/deepseek-v4.1-flash-1017",
    messages=[{"role": "user", "content": "What is the capital of France? Answer in one word."}],
    logprobs=True,
    top_logprobs=3
)

for item in response.choices[0].logprobs.content:
    print(f"{item.token!r}: {math.exp(item.logprob):.1%}")
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://api.tensorx.ai/v1'
});

const response = await client.chat.completions.create({
  model: 'deepseek/deepseek-v4.1-flash-1017',
  messages: [{ role: 'user', content: 'What is the capital of France? Answer in one word.' }],
  logprobs: true,
  top_logprobs: 3
});

for (const item of response.choices[0].logprobs.content) {
  console.log(`${JSON.stringify(item.token)}: ${(Math.exp(item.logprob) * 100).toFixed(1)}%`);
}

This model does not reason by default. If you use a model that does, see Reasoning Models.


Response Format

Each choice carries a logprobs object. Its content array has one entry for each output token:

Field
Type
Description

choices[].logprobs.content

array

One entry for each output token, in order

content[].token

string

The token text

content[].logprob

number

The natural log of the token's probability

content[].bytes

array

The UTF-8 bytes of the token

content[].top_logprobs

array

The most likely tokens at this position, each with token, logprob and bytes. Present when you set top_logprobs

To turn a logprob into a probability, take its exponential: math.exp(logprob) in Python or Math.exp(logprob) in JavaScript. A logprob of 0 means that the model was certain. A logprob of -0.69 is about 50% and -2.3 is about 10%. In the example, the model gave Paris a probability above 99.99%. The next most likely first token, The, has a logprob of about -15.4, which is close to zero.

The last entry, <|end▁of▁sentence|>, is the model's end-of-turn token. See The Last Entry Can Be a Stop Token.


Example: Confidence Threshold

A common use is to let a workflow act on its own when the model is sure, and to send the case to a person when it is not. This example checks whether an invoice is a duplicate:

Set the two thresholds from your own data. For example, run the check over invoices that a person has already reviewed and pick the values where the automatic decisions were correct.


Streaming

With stream: true, each chunk carries the logprobs for the tokens in that chunk:


Things to Know

Reasoning Models

When a model reasons before it answers, logprobs.content covers the reasoning tokens as well as the answer. The entries for the reasoning come first, then the end-of-reasoning marker (</think> on GLM models), then the answer.

The reasoning also uses the max_tokens budget. With a small max_tokens, such as the single token in the confidence example, the budget runs out before the answer starts.

Turn thinking off when you need logprobs for the answer only. The control differs by model family. See Reasoning.

The Last Entry Can Be a Stop Token

The last entry in logprobs.content can be the token that ended the model's turn, such as <|end▁of▁sentence|> or <|user|>. The token depends on the model. It is not part of message.content. Leave it out when you score an answer.

Tokens Are Pieces of Words

A token is often part of a word, and the same word can come back in different forms, such as yes, yes or Yes. Normalise the tokens and add up their probabilities before you compare them, as the confidence example does.

Values Depend on the Deployment

The values can differ from the same model on another platform, because of the model's quantisation and serving configuration. They can also change when TensorX updates a model's deployment. Set thresholds from TensorX responses, not from values measured elsewhere, and check them again after a model update.

Check Before You Read

A model that does not support logprobs does not return them. Check that choices[].logprobs is present before you read it.


See Also

Last updated

Was this helpful?