_reduce_tokens_below_limit() — langchain Function Reference
Architecture documentation for the _reduce_tokens_below_limit() function in retrieval.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 0afcca8a_ef8b_63ff_50cb_7330bdb61fee["_reduce_tokens_below_limit()"] 76f853c5_b279_fd04_f253_b0c960712f80["RetrievalQAWithSourcesChain"] 0afcca8a_ef8b_63ff_50cb_7330bdb61fee -->|defined in| 76f853c5_b279_fd04_f253_b0c960712f80 857136aa_95ce_637d_eb84_ed2df24dae5c["_get_docs()"] 857136aa_95ce_637d_eb84_ed2df24dae5c -->|calls| 0afcca8a_ef8b_63ff_50cb_7330bdb61fee a9a5b4e9_e1f3_681e_a5ee_75ece394ee79["_aget_docs()"] a9a5b4e9_e1f3_681e_a5ee_75ece394ee79 -->|calls| 0afcca8a_ef8b_63ff_50cb_7330bdb61fee style 0afcca8a_ef8b_63ff_50cb_7330bdb61fee fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/qa_with_sources/retrieval.py lines 28–44
def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
num_docs = len(docs)
if self.reduce_k_below_max_tokens and isinstance(
self.combine_documents_chain,
StuffDocumentsChain,
):
tokens = [
self.combine_documents_chain.llm_chain._get_num_tokens(doc.page_content) # noqa: SLF001
for doc in docs
]
token_count = sum(tokens[:num_docs])
while token_count > self.max_tokens_limit:
num_docs -= 1
token_count -= tokens[num_docs]
return docs[:num_docs]
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does _reduce_tokens_below_limit() do?
_reduce_tokens_below_limit() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/qa_with_sources/retrieval.py.
Where is _reduce_tokens_below_limit() defined?
_reduce_tokens_below_limit() is defined in libs/langchain/langchain_classic/chains/qa_with_sources/retrieval.py at line 28.
What calls _reduce_tokens_below_limit()?
_reduce_tokens_below_limit() is called by 2 function(s): _aget_docs, _get_docs.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free