LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    โŒ˜I

    Chat LangChain

    What can I help with?

    Enter to sendยทShift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corelanguage_modelsbaseBaseLanguageModelget_num_tokens
    Methodโ—Since v1.8

    get_num_tokens

    Copy
    get_num_tokens(
        self,
        text: str,
    ) -> int
    View source on GitHub

    Parameters

    NameTypeDescription
    text*str

    The string input to tokenize.

    Get the number of tokens present in the text.

    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.