{"record":{"id":"c19b19518b2c0236","repo":"BerriAI/litellm","slug":"failed-to-update-cost-margin-config-e","errorCode":null,"errorMessage":"Failed to update cost margin config: {e}","messagePattern":"Failed to update cost margin config: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"litellm/proxy/management_endpoints/cost_tracking_settings.py","lineNumber":436,"sourceCode":"        # Update cost_margin_config\n        config[\"litellm_settings\"][\"cost_margin_config\"] = cost_margin_config\n\n        # Save the updated config to DB\n        await proxy_config.save_config(new_config=config)\n\n        # Update in-memory litellm.cost_margin_config\n        litellm.cost_margin_config = cost_margin_config\n\n        verbose_proxy_logger.info(\"Updated cost_margin_config: %s\", cost_margin_config)\n\n        return {\n            \"message\": \"Cost margin configuration updated successfully\",\n            \"status\": \"success\",\n            \"values\": cost_margin_config,\n        }\n    except Exception as e:\n        verbose_proxy_logger.error(\"Error updating cost margin config: %s\", e)\n        raise HTTPException(\n            status_code=500,\n            detail={\"error\": f\"Failed to update cost margin config: {e}\"},\n        )\n\n\n@router.post(\n    \"/cost/estimate\",\n    tags=[\"Cost Tracking\"],\n    dependencies=[Depends(user_api_key_auth)],\n    response_model=CostEstimateResponse,\n)\nasync def estimate_cost(\n    request: CostEstimateRequest,\n    user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),\n) -> CostEstimateResponse:\n    \"\"\"\n    Estimate cost for a given model and token counts.\n","sourceCodeStart":418,"sourceCodeEnd":454,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/management_endpoints/cost_tracking_settings.py#L418-L454","documentation":"Catch-all HTTP 500 from PATCH /config/cost_margin_config: all validation passed, but the subsequent config load/save (proxy_config.get_config / save to DB) or the in-memory update (litellm.cost_margin_config = ...) raised. The original exception is logged as 'Error updating cost margin config' and embedded in the response detail so the real cause is visible.","triggerScenarios":"Prisma write failures (DB restarted, connection pool exhausted, permissions) during save_config; corrupt existing config JSON causing merge errors; races with another concurrent config update.","commonSituations":"Transient DB outages at the moment of the PATCH; a config table row that was hand-edited and no longer parses; multiple operators/automation updating config simultaneously.","solutions":["Read the embedded {e} text — it carries the underlying exception and points at DB vs config parsing.","Restore/verify DB connectivity, then retry the PATCH (failed saves leave the old config intact).","Fetch GET /config/cost_margin_config (or the raw config) to confirm state before and after retries.","If corruption is indicated, repair the stored config row and re-apply."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    r = requests.patch(f\"{PROXY_URL}/config/cost_margin_config\", json=payload, headers=HDRS, timeout=15)\n    r.raise_for_status()\nexcept requests.HTTPError as e:\n    detail = e.response.json().get(\"detail\", {}).get(\"error\", \"\")\n    if e.response.status_code == 500 and \"Failed to update cost margin config\" in detail:\n        log.error(\"root cause from proxy: %s\", detail)\n        current = requests.get(f\"{PROXY_URL}/config/cost_margin_config\", headers=HDRS).json()\n        decide_retry_or_alert(current)  # state-aware recovery instead of blind retry\n    else:\n        raise","preventionTips":["Parse the embedded {e} to separate DB failures from config-parse failures.","Verify applied state with a follow-up GET after any 500.","Serialize config mutations (single writer) to avoid concurrent-update races."],"tags":["litellm-proxy","database","cost-tracking","internal-error"],"backgroundTag":"config-update-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}