Skip to content

feat(hosted key): Add hosted key, exa as shared key4.#3221

Open
TheodoreSpeaks wants to merge 13 commits intostagingfrom
feat/sim-provided-key
Open

feat(hosted key): Add hosted key, exa as shared key4.#3221
TheodoreSpeaks wants to merge 13 commits intostagingfrom
feat/sim-provided-key

Conversation

@TheodoreSpeaks
Copy link
Collaborator

Summary

Added hosted api key integration for exa search.
For hosted sim, api sub block no longer appears for exa search. Users can use the BYOK modal to pass an api key if needed. Non-hosted sim behaves unchanged.

Hosted key calls retry 3 times with exponential backoff to handle throttles from increased load.

Important: 3 api keys for exa ai need to be stored in prod before this can be merged

EXA_API_KEY_1, EXA_API_KEY_2, EXA_API_KEY_3

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

  • Tested non-host still has required api subblock.
  • Tested hosted sim does not have api subblock
  • Tested hosted key updates user stats and usage log tables correctly
  • Unit tested throttling handles correctly

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Screenshots/Videos

@vercel
Copy link

vercel bot commented Feb 14, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Feb 14, 2026 4:56pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 14, 2026

Greptile Overview

Greptile Summary

Added hosted API key integration for Exa search tools, enabling Sim to provide shared API keys for users while tracking usage and costs.

Key Changes:

  • Implemented hosted key injection with priority: BYOK workspace keys → hosted environment keys → user error
  • Added round-robin key rotation across 3 environment variables (EXA_API_KEY_1, EXA_API_KEY_2, EXA_API_KEY_3) based on current minute
  • Implemented exponential backoff retry logic (3 attempts, 1s/2s/4s delays) to handle rate limiting from increased load
  • Extended all 5 Exa tools (search, answer, research, get_contents, find_similar_links) with hosting configuration
  • Added custom pricing that extracts actual cost from Exa API costDollars response with fallback estimates
  • Integrated usage tracking to usageLog table and userStats for billing
  • Hidden API key subblock in hosted environments via hideWhenHosted flag
  • Added Exa to BYOK settings UI for workspace-level key management
  • Comprehensive test coverage for retry logic, cost calculation, and key injection

Important:
The PR description notes that 3 API keys (EXA_API_KEY_1, EXA_API_KEY_2, EXA_API_KEY_3) must be configured in production before merging.

Confidence Score: 4/5

  • This PR is safe to merge after environment variables are configured, with well-tested core logic and comprehensive error handling
  • The implementation is thorough with strong test coverage (750+ new test lines), proper error handling, and clear separation of concerns. Deducted one point due to the production dependency on environment variables that must be configured before deployment.
  • Ensure EXA_API_KEY_1, EXA_API_KEY_2, and EXA_API_KEY_3 are configured in production environment before merging

Important Files Changed

Filename Overview
apps/sim/tools/index.ts Added hosted key injection with retry logic, cost calculation, and usage tracking - core implementation looks solid with good test coverage
apps/sim/tools/types.ts Added comprehensive type definitions for hosted key config and pricing models
apps/sim/tools/exa/search.ts Added hosting config with custom pricing based on Exa API costDollars response, includes fallback pricing
apps/sim/blocks/blocks/exa.ts Added hideWhenHosted: true to API key subblock to hide field in hosted environments
apps/sim/lib/workflows/subblocks/visibility.ts Added isSubBlockHiddenByHostedKey function to check hideWhenHosted flag against isHosted feature flag
apps/sim/lib/core/telemetry.ts Added hostedKeyThrottled event to track rate limiting events during retry attempts
apps/sim/lib/billing/core/usage-log.ts Extended FixedUsageMetadata to support Record<string, unknown> and added optional metadata field to logFixedUsage
apps/sim/tools/index.test.ts Added comprehensive test suites for hosted key injection, retry logic, rate limiting, and cost calculation

Flowchart

flowchart TD
    Start[User executes Exa tool] --> CheckHosted{Is hosted Sim?}
    CheckHosted -->|No| UseUserKey[Use user-provided API key<br/>No billing]
    CheckHosted -->|Yes| HideUI[API key field hidden in UI]
    HideUI --> CheckBYOK{BYOK key exists?}
    CheckBYOK -->|Yes| UseBYOK[Use BYOK workspace key<br/>No billing]
    CheckBYOK -->|No| CheckEnv{Hosted env keys<br/>configured?}
    CheckEnv -->|No| Fail[Execution fails]
    CheckEnv -->|Yes| RotateKey[Round-robin key rotation<br/>based on current minute]
    RotateKey --> InjectKey[Inject hosted API key]
    InjectKey --> Execute[Execute tool request]
    Execute --> CheckError{Rate limit error<br/>429/503?}
    CheckError -->|Yes, retries left| Backoff[Exponential backoff<br/>1s, 2s, 4s]
    Backoff --> LogThrottle[Log throttling event<br/>to telemetry]
    LogThrottle --> Execute
    CheckError -->|Yes, no retries| Fail
    CheckError -->|No| Success{Success?}
    Success -->|Yes| CalcCost[Calculate cost from<br/>API response or fallback]
    CalcCost --> LogUsage[Log to usageLog table<br/>and userStats]
    LogUsage --> AddCost[Add cost field to output]
    AddCost --> StripInternal[Strip internal fields<br/>starting with _]
    StripInternal --> Return[Return result to user]
    Success -->|No| Fail
    UseBYOK --> Execute
    UseUserKey --> Execute
Loading

Last reviewed commit: fbd1cdf

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.

22 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

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.

1 participant