Skip to content

Hotfix/AI post limits deploy fixes#461

Draft
dspachos wants to merge 11 commits into
devfrom
hotfix/AI-post-limits-deploy-fixes
Draft

Hotfix/AI post limits deploy fixes#461
dspachos wants to merge 11 commits into
devfrom
hotfix/AI-post-limits-deploy-fixes

Conversation

@dspachos
Copy link
Copy Markdown
Contributor

@dspachos dspachos commented May 5, 2026

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR delivers hotfixes around spend-cap persistence/synchronization with LiteLLM and adds an operational script to clean up “orphaned” LiteLLM keys referenced from the DB.

Changes:

  • Add a one-off script to detect LiteLLM keys that no longer exist remotely and optionally null out their DB references (plus remove associated spend_caps).
  • Fix spend_caps lookup/upsert/delete helpers to match the spend_caps partial unique-index columns and repair stale team/user metadata to avoid unique constraint violations.
  • Deprecate the legacy private-ai-keys spend endpoint and adjust it to prefer the configured DB spend cap when present.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
scripts/detect_orphaned_keys.py New maintenance script to detect orphaned LiteLLM tokens and optionally clean up DB state + spend_caps.
scripts/backfill_litellm_sync.py Formatting-only change to add_team_member call for readability.
app/api/spend.py Align spend_caps lookups with partial unique indexes; repair stale columns on update to prevent duplicate inserts.
app/api/private_ai_keys.py Deprecate legacy spend endpoint and override returned max_budget from spend_caps when configured.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread scripts/detect_orphaned_keys.py Outdated
Comment thread scripts/detect_orphaned_keys.py Outdated
Comment thread scripts/detect_orphaned_keys.py
Comment thread app/api/private_ai_keys.py Outdated
Comment thread app/api/spend.py Outdated
Comment thread scripts/detect_orphaned_keys.py Outdated
@dspachos
Copy link
Copy Markdown
Contributor Author

dspachos commented May 5, 2026

@copilot apply changes based on the comments in this thread

…okup

- Redact litellm_token in report output (store only last 4 chars hint as
  litellm_token_hint) to avoid writing secrets to disk
- Apply --limit at DB query level instead of after .all() to bound DB load
- Update docstring to reflect both 401 and 404 are treated as orphaned
- Preload regions and reuse LiteLLMService per region to avoid N+1 DB pattern
- Fix key-scope spend_cap lookup in private_ai_keys.py to use (region_id, key_id)
  matching the unique index uq_spend_caps_key_scope instead of filtering on
  team_id/user_id which are metadata and can be stale/NULL
- Add regression test for stale-column repair path in _upsert_spend_cap to
  verify it finds existing row by (region_id, key_id) and repairs team_id/
  user_id in-place without UniqueViolation

Agent-Logs-Url: https://github.com/amazeeio/amazee.ai/sessions/e02b1833-5c0a-44ee-bbfe-84aa4855e49c

Co-authored-by: dspachos <6309422+dspachos@users.noreply.github.com>

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants