Skip to content

Debug auto top up feature not working#5533

Merged
chitalian merged 1 commit intomainfrom
claude/debug-auto-topup-lnMaw
Jan 21, 2026
Merged

Debug auto top up feature not working#5533
chitalian merged 1 commit intomainfrom
claude/debug-auto-topup-lnMaw

Conversation

@chitalian
Copy link
Contributor

…rors

When PTB requests fail due to insufficient credits, the auto-topoff check was never triggered because:

  1. The checkAndScheduleAutoTopoffAlarm() call only existed in ProxyForwarder.ts
  2. Failed escrow reservations returned early without reaching that code path

Now when escrow fails with "insufficient_credit_limit", we trigger the auto-topoff alarm check. This ensures users with auto-topoff configured will have their accounts recharged even when requests are being rejected.

Ticket

Link to the ticket(s) this pull request addresses.

Component/Service

What part of Helicone does this affect?

  • Web (Frontend)
  • Jawn (Backend)
  • Worker (Proxy)
  • Bifrost (Marketing)
  • AI Gateway
  • Packages
  • Infrastructure/Docker
  • Documentation

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Performance improvement
  • Refactoring

Deployment Notes

  • No special deployment steps required
  • Database migrations need to run
  • Environment variable changes required
  • Coordination with other teams needed

Screenshots / Demos

Before After

Extra Notes

Any additional context, considerations, or notes for reviewers.

Context

Why are you making this change?

Screenshots / Demos

…rors

When PTB requests fail due to insufficient credits, the auto-topoff check
was never triggered because:
1. The checkAndScheduleAutoTopoffAlarm() call only existed in ProxyForwarder.ts
2. Failed escrow reservations returned early without reaching that code path

Now when escrow fails with "insufficient_credit_limit", we trigger the
auto-topoff alarm check. This ensures users with auto-topoff configured
will have their accounts recharged even when requests are being rejected.
@vercel
Copy link

vercel bot commented Jan 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

3 Skipped Deployments
Project Deployment Review Updated (UTC)
helicone Skipped Skipped Jan 21, 2026 3:57am
helicone-bifrost Skipped Skipped Jan 21, 2026 3:57am
helicone-eu Skipped Skipped Jan 21, 2026 3:57am

Request Review

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@supabase
Copy link

supabase bot commented Jan 21, 2026

This pull request has been ignored for the connected project bolqqmqbrciybnypvklh because there are no changes detected in supbase/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@supabase
Copy link

supabase bot commented Jan 21, 2026

This pull request has been ignored for the connected project lmahfbbnchpworytrrqk because there are no changes detected in .supabase/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@claude
Copy link
Contributor

claude bot commented Jan 21, 2026

Claude finished @chitalian's task —— View job


PR Review: Debug auto top up feature not working

Todo List:

  • Read and analyze the modified file
  • Examine the specific changes made
  • Review code for bugs, security issues, and quality
  • Provide comprehensive feedback with confidence score

Analysis

Change Summary

This PR adds auto-topoff alarm scheduling when escrow reservations fail due to insufficient credits. The specific change is in worker/src/lib/ai-gateway/AttemptExecutor.ts at lines 154-162, where a new conditional block triggers auto-topoff checking when awaitedEscrow.error.type === "insufficient_credit_limit".

Score: 8/10 - Good to merge, minor suggestions

Code Quality Assessment

Strengths

  • Correct Problem Identification: The fix correctly addresses the issue described in the PR - auto-topoff wasn't triggered when requests were rejected due to insufficient credits
  • Proper Error Handling: The code correctly checks for the specific error type "insufficient_credit_limit" which is properly defined in the type system
  • Consistent Pattern: Uses the same pattern as ProxyForwarder.ts:273 for calling the auto-topoff functionality
  • Non-blocking Execution: Properly uses this.ctx.waitUntil() to ensure the auto-topoff check doesn't block the current request
  • Appropriate Placement: The check is placed in the correct location within the error handling flow

Security & Performance

  • No Security Issues: The change doesn't introduce any security vulnerabilities
  • Performance Impact: Minimal - only adds a few lines executed on error conditions
  • Resource Management: Uses waitUntil() correctly for background processing

📋 Minor Suggestions

  1. Code Organization (worker/src/lib/ai-gateway/AttemptExecutor.ts:156-162):
    The auto-topoff logic could be extracted into a helper method for better testability:

    private triggerAutoTopoffCheck(orgId: string): void {
      const walletId = this.env.WALLET.idFromName(orgId);
      const walletStub = this.env.WALLET.get(walletId);
      this.ctx.waitUntil(
        walletStub.checkAndScheduleAutoTopoffAlarm(orgId)
      );
    }
  2. Error Handling Enhancement: Consider adding error handling around the auto-topoff call:

    this.ctx.waitUntil(
      walletStub.checkAndScheduleAutoTopoffAlarm(props.orgId)
        .catch(error => console.error('Auto-topoff scheduling failed:', error))
    );
  3. Logging: Adding a debug log could help with troubleshooting:

    if (awaitedEscrow.error.type === "insufficient_credit_limit") {
      console.log(`Triggering auto-topoff check for org: ${props.orgId}`);
      // ... existing code
    }

Suggestions Summary:

  • Consider extracting auto-topoff logic into a helper method for better organization
  • Add error handling around the background auto-topoff call
  • Consider adding debug logging for troubleshooting purposes

Technical Verification

  • ✅ Type safety: insufficient_credit_limit is properly defined in AttemptError type
  • ✅ API consistency: Uses same wallet interaction pattern as existing code
  • ✅ Error flow: Correctly placed in the error handling path after escrow failure

Overall Assessment: This is a clean, focused fix that addresses a specific issue without introducing complexity. The implementation follows existing patterns and maintains code quality standards.

@chitalian chitalian merged commit 7942de4 into main Jan 21, 2026
10 of 11 checks passed
@chitalian chitalian deleted the claude/debug-auto-topup-lnMaw branch January 21, 2026 04:13
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.

2 participants