get_num_tokens_from_messages() — langchain Function Reference
Architecture documentation for the get_num_tokens_from_messages() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 20140000_1d28_439f_e76d_4451abf1068a["get_num_tokens_from_messages()"] f5a29046_740d_1358_3615_8cc689b26ca9["BaseLanguageModel"] 20140000_1d28_439f_e76d_4451abf1068a -->|defined in| f5a29046_740d_1358_3615_8cc689b26ca9 4b493e08_cb6e_df06_b3b7_c129ab4710fd["get_num_tokens()"] 20140000_1d28_439f_e76d_4451abf1068a -->|calls| 4b493e08_cb6e_df06_b3b7_c129ab4710fd style 20140000_1d28_439f_e76d_4451abf1068a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/language_models/base.py lines 338–373
def get_num_tokens_from_messages(
self,
messages: list[BaseMessage],
tools: Sequence | None = None,
) -> int:
"""Get the number of tokens in the messages.
Useful for checking if an input fits in a model's context window.
This should be overridden by model-specific implementations to provide accurate
token counts via model-specific tokenizers.
!!! note
* The base implementation of `get_num_tokens_from_messages` ignores tool
schemas.
* The base implementation of `get_num_tokens_from_messages` adds additional
prefixes to messages in represent user roles, which will add to the
overall token count. Model-specific implementations may choose to
handle this differently.
Args:
messages: The message inputs to tokenize.
tools: If provided, sequence of dict, `BaseModel`, function, or
`BaseTool` objects to be converted to tool schemas.
Returns:
The sum of the number of tokens across the messages.
"""
if tools is not None:
warnings.warn(
"Counting tokens in tool schemas is not yet supported. Ignoring tools.",
stacklevel=2,
)
return sum(self.get_num_tokens(get_buffer_string([m])) for m in messages)
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does get_num_tokens_from_messages() do?
get_num_tokens_from_messages() is a function in the langchain codebase, defined in libs/core/langchain_core/language_models/base.py.
Where is get_num_tokens_from_messages() defined?
get_num_tokens_from_messages() is defined in libs/core/langchain_core/language_models/base.py at line 338.
What does get_num_tokens_from_messages() call?
get_num_tokens_from_messages() calls 1 function(s): get_num_tokens.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free