{"record":{"id":"0ecf8aca0f88d258","repo":"BerriAI/litellm","slug":"limit-must-be-non-negative","errorCode":null,"errorMessage":"limit must be non-negative","messagePattern":"limit must be non-negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/integrations/focus/database.py","lineNumber":57,"sourceCode":"            query_params.append(start_time_utc)\n            placeholder_index += 1\n        if end_time_utc:\n            where_clauses.append(f\"dus.updated_at <= ${placeholder_index}::timestamptz\")\n            query_params.append(end_time_utc)\n            placeholder_index += 1\n\n        where_clause = \"\"\n        if where_clauses:\n            where_clause = \"WHERE \" + \" AND \".join(where_clauses)\n\n        limit_clause = \"\"\n        if limit is not None:\n            try:\n                limit_value: Final = int(limit)\n            except (TypeError, ValueError) as exc:  # pragma: no cover - defensive guard\n                raise ValueError(\"limit must be an integer\") from exc\n            if limit_value < 0:\n                raise ValueError(\"limit must be non-negative\")\n            limit_clause = f\" LIMIT ${placeholder_index}\"\n            query_params.append(limit_value)\n\n        query: Final = f\"\"\"\n        SELECT\n            dus.id,\n            dus.date,\n            dus.user_id,\n            dus.api_key,\n            dus.model,\n            dus.model_group,\n            dus.custom_llm_provider,\n            dus.prompt_tokens,\n            dus.completion_tokens,\n            dus.spend,\n            dus.api_requests,\n            dus.successful_requests,\n            dus.failed_requests,","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/integrations/focus/database.py#L39-L75","documentation":"After successfully converting limit to an int, get_usage_data rejects values below zero with ValueError('limit must be non-negative'). The value is then interpolated into the SQL LIMIT clause, so the guard prevents a malformed/negative LIMIT from reaching Postgres.","triggerScenarios":"get_usage_data(limit=-1); limit computed as a subtraction that underflows (e.g. page_size - offset); config defaulting to -1 as an 'unlimited' sentinel — this API uses None for unlimited, not -1.","commonSituations":"Porting code where -1 meant 'no limit'; user-supplied pagination input not clamped; arithmetic on defaults producing negatives.","solutions":["Use limit=None when you want no LIMIT clause (unlimited)","Clamp user input: max(0, requested_limit)","Validate pagination math (offset/size) before calling the export API"],"exampleFix":"# before\ndf = await db.get_usage_data(limit=page_size - offset)  # can go negative\n\n# after\ndf = await db.get_usage_data(limit=max(0, page_size - offset) if page_size > offset else None)","handlingStrategy":"validation","validationCode":"limit = None if user_limit is None else max(0, int(user_limit))\ndf = await db.get_usage_data(limit=limit)","typeGuard":null,"tryCatchPattern":"try:\n    df = await db.get_usage_data(limit=limit)\nexcept ValueError as e:\n    if 'limit must be non-negative' in str(e):\n        df = await db.get_usage_data(limit=abs(limit))\n    else:\n        raise","preventionTips":["Use None (not -1) as the 'unlimited' sentinel — the API maps None to no LIMIT clause","Clamp pagination arithmetic results before passing them in","Validate user-supplied limit ranges at the request boundary"],"tags":["litellm","focus","validation","argument-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}