{"record":{"id":"0aa01498a9664e17","repo":"run-llama/llama_index","slug":"calculated-available-context-size-context-size-to","errorCode":null,"errorMessage":"Calculated available context size {context_size_tokens} was not non-negative.","messagePattern":"Calculated available context size (.+?) was not non-negative\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/indices/prompt_helper.py","lineNumber":161,"sourceCode":"    def class_name(cls) -> str:\n        return \"PromptHelper\"\n\n    def _get_available_context_size(self, num_prompt_tokens: int) -> int:\n        \"\"\"\n        Get available context size.\n\n        This is calculated as:\n            available context window = total context window\n                - input (partially filled prompt)\n                - output (room reserved for response)\n\n        Notes:\n        - Available context size is further clamped to be non-negative.\n\n        \"\"\"\n        context_size_tokens = self.context_window - num_prompt_tokens - self.num_output\n        if context_size_tokens < 0:\n            raise ValueError(\n                f\"Calculated available context size {context_size_tokens} was\"\n                \" not non-negative.\"\n            )\n        return context_size_tokens\n\n    def _get_tools_from_llm(\n        self, llm: Optional[LLM] = None, tools: Optional[List[\"BaseTool\"]] = None\n    ) -> List[\"BaseTool\"]:\n        from llama_index.core.program.function_program import get_function_tool\n\n        tools = tools or []\n        if isinstance(llm, StructuredLLM):\n            tools.append(get_function_tool(llm.output_cls))\n\n        return tools\n\n    def _get_available_chunk_size(\n        self,","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/indices/prompt_helper.py#L143-L179","documentation":"PromptHelper._get_available_context_size computes context_window - num_prompt_tokens - num_output and raises when the result is negative. It means the prompt you already built plus the tokens reserved for the model output exceed the configured context window, leaving negative room for any content.","triggerScenarios":"Calling get_text_splitter / _get_available_chunk_size (directly or via index construction with a long prompt) where num_prompt_tokens + num_output > context_window. Typical with small context_window (e.g. 2048 for older models) combined with a large num_output (e.g. 1024+) or a verbose prompt template.","commonSituations":"Using a local/small-window model but leaving Settings.llm.num_output or PromptHelper defaults sized for large models; a very long system/custom prompt; a tokenizer mismatch that over-counts prompt tokens.","solutions":["Increase context_window (e.g. service_context = ServiceContext.from_defaults(llm=llm, context_window=8192)) or use a model with a larger window","Lower num_output so context_window - num_output leaves room for the prompt","Shorten the prompt template or reduce the number/format of few-shot examples","If the tokenizer is over-counting, supply the correct tokenizer via a custom num_output/token counter in PromptHelper"],"exampleFix":"# before\nfrom llama_index.core import PromptHelper\nhelper = PromptHelper(context_window=2048, num_output=1536, chunk_overlap_ratio=0.1)\n\n# after\nhelper = PromptHelper(context_window=8192, num_output=512, chunk_overlap_ratio=0.1)","handlingStrategy":"validation","validationCode":"from llama_index.core import PromptHelper\nnum_prompt_tokens = helper._token_counter(prompt)  # or llm.get_token_cnt(prompt)\nif helper.context_window - num_prompt_tokens - helper.num_output < 0:\n    raise ValueError('prompt + num_output exceeds context_window; shrink prompt or raise context_window')","typeGuard":null,"tryCatchPattern":"try:\n    splitter = prompt_helper.get_text_splitter(prompt, num_chunks)\nexcept ValueError as e:\n    if 'not non-negative' in str(e):\n        # reconfigure with a larger window / smaller num_output and retry once\n        prompt_helper = PromptHelper(context_window=8192, num_output=256)\n    else:\n        raise","preventionTips":["Set context_window explicitly to the model's real window when constructing the LLM/PromptHelper","Keep num_output modest relative to context_window","Log num_prompt_tokens for your prompts once at startup to catch budget overruns early"],"tags":["llama-index","prompt","context-window","configuration"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}