BerriAI/litellm · error · HTTPException

Failed to update key spend

Error message

Failed to update key spend

What it means

Error "Failed to update key spend" thrown in BerriAI/litellm.

Solutions

  1. Check proxy logs for the underlying database error.
  2. Verify the key exists and the database connection is healthy, then retry.

When it happens

Trigger: Thrown at litellm/proxy/management_endpoints/key_management_endpoints.py:5200 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/ba1c6591cd3194f8. Report an issue: GitHub.

Appendix: source

Thrown at litellm/proxy/management_endpoints/key_management_endpoints.py:5200

            )

        current_spend: Final = _key_in_db.spend or 0.0
        reset_to: Final = _validate_reset_spend_value(data.reset_to, _key_in_db)

        await _check_proxy_or_team_admin_for_key(
            key_in_db=_key_in_db,
            user_api_key_dict=user_api_key_dict,
            prisma_client=prisma_client,
            user_api_key_cache=user_api_key_cache,
        )

        updated_key: Final = await _prisma_table(VerificationTokenRepository(prisma_client)).update(
            where={"token": hashed_api_key},
            data={"spend": reset_to},
        )

        if updated_key is None:
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail={"error": "Failed to update key spend"},
            )

        await _delete_cache_key_object(
            hashed_token=hashed_api_key,
            user_api_key_cache=user_api_key_cache,
            proxy_logging_obj=proxy_logging_obj,
        )

        # Set Redis spend counter to the new value so get_current_spend()
        # returns the correct amount immediately instead of the stale pre-reset value.
        # We use reset_to (not 0.0) so partial resets are reflected correctly.
        from litellm.proxy.proxy_server import spend_counter_cache

        _counter_key: Final = f"spend:key:{hashed_api_key}"
        spend_counter_cache.in_memory_cache.set_cache(key=_counter_key, value=reset_to, ttl=60)
        if spend_counter_cache.redis_cache is not None:

View on GitHub (pinned to 77b7c6c40c)