*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to get log probabilities

On this page

How to get log probabilities
============================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Chat models](/v0.2/docs/concepts/#chat-models)

Certain chat models can be configured to return token-level log probabilities representing the likelihood of a given token. This guide walks through how to get this information in LangChain.

OpenAI[​](#openai "Direct link to OpenAI")
------------------------------------------

Install the LangChain x OpenAI package and set your API key

    %pip install -qU langchain-openai

    import getpassimport osos.environ["OPENAI_API_KEY"] = getpass.getpass()

For the OpenAI API to return log probabilities we need to configure the `logprobs=True` param. Then, the logprobs are included on each output [`AIMessage`](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.ai.AIMessage.html) as part of the `response_metadata`:

    from langchain_openai import ChatOpenAIllm = ChatOpenAI(model="gpt-3.5-turbo-0125").bind(logprobs=True)msg = llm.invoke(("human", "how are you today"))msg.response_metadata["logprobs"]["content"][:5]

**API Reference:**[ChatOpenAI](https://api.python.langchain.com/en/latest/chat_models/langchain_openai.chat_models.base.ChatOpenAI.html)

    [{'token': 'I', 'bytes': [73], 'logprob': -0.26341408, 'top_logprobs': []}, {'token': "'m",  'bytes': [39, 109],  'logprob': -0.48584133,  'top_logprobs': []}, {'token': ' just',  'bytes': [32, 106, 117, 115, 116],  'logprob': -0.23484154,  'top_logprobs': []}, {'token': ' a',  'bytes': [32, 97],  'logprob': -0.0018291725,  'top_logprobs': []}, {'token': ' computer',  'bytes': [32, 99, 111, 109, 112, 117, 116, 101, 114],  'logprob': -0.052299336,  'top_logprobs': []}]

And are part of streamed Message chunks as well:

    ct = 0full = Nonefor chunk in llm.stream(("human", "how are you today")):    if ct < 5:        full = chunk if full is None else full + chunk        if "logprobs" in full.response_metadata:            print(full.response_metadata["logprobs"]["content"])    else:        break    ct += 1

    [][{'token': 'I', 'bytes': [73], 'logprob': -0.26593843, 'top_logprobs': []}][{'token': 'I', 'bytes': [73], 'logprob': -0.26593843, 'top_logprobs': []}, {'token': "'m", 'bytes': [39, 109], 'logprob': -0.3238896, 'top_logprobs': []}][{'token': 'I', 'bytes': [73], 'logprob': -0.26593843, 'top_logprobs': []}, {'token': "'m", 'bytes': [39, 109], 'logprob': -0.3238896, 'top_logprobs': []}, {'token': ' just', 'bytes': [32, 106, 117, 115, 116], 'logprob': -0.23778509, 'top_logprobs': []}][{'token': 'I', 'bytes': [73], 'logprob': -0.26593843, 'top_logprobs': []}, {'token': "'m", 'bytes': [39, 109], 'logprob': -0.3238896, 'top_logprobs': []}, {'token': ' just', 'bytes': [32, 106, 117, 115, 116], 'logprob': -0.23778509, 'top_logprobs': []}, {'token': ' a', 'bytes': [32, 97], 'logprob': -0.0022134194, 'top_logprobs': []}]

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You've now learned how to get logprobs from OpenAI models in LangChain.

Next, check out the other how-to guides chat models in this section, like [how to get a model to return structured output](/v0.2/docs/how_to/structured_output/) or [how to track token usage](/v0.2/docs/how_to/chat_token_usage_tracking/).

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/logprobs.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

Run LLMs locally

](/v0.2/docs/how_to/local_llms/)[

Next

How to reorder retrieved results to mitigate the "lost in the middle" effect

](/v0.2/docs/how_to/long_context_reorder/)

*   [OpenAI](#openai)
*   [Next steps](#next-steps)