Skip to content

Add concurrency limiter for Semantic Scholar API requests #212

@tnkshuuhei

Description

@tnkshuuhei

Problem

When external paper search is enabled, each under-matched edge triggers 1 LLM call (Gemini 2.5 Flash for query extraction) + 2 Semantic Scholar API requests in parallel. With Promise.allSettled in the workflow step, all edges are searched concurrently.

For a logic model with 15 under-matched edges, this fires 15 LLM calls + 30 Semantic Scholar API requests simultaneously. Semantic Scholar's free tier allows ~100 requests per 5 minutes, so larger logic models can easily hit 429 rate limit errors.

Because Promise.allSettled swallows these errors gracefully, the user experience degrades silently — the toggle is enabled but most edges return 0 external papers.

Proposed Solution

Add a concurrency limiter to searchExternalPapersStep in mastra/workflows/logic-model-with-evidence.ts or within lib/external-paper-search.ts.

Options:

  • Batch processing: Process N edges at a time (e.g., 3 concurrent edge searches)
  • Library-based: Use p-limit or similar to cap concurrent requests
  • Built-in backoff: Add retry with exponential backoff on 429 responses in lib/academic-apis/semantic-scholar.ts

Relevant Files

  • mastra/workflows/logic-model-with-evidence.ts — Step 2.5 (searchExternalPapersStep)
  • lib/external-paper-search.tssearchExternalPapersForEdge()
  • lib/academic-apis/semantic-scholar.tsexecuteSearch()

Context

Identified during review of PR #194. Not blocking for merge since the feature is opt-in (requires both NEXT_PUBLIC_EXTERNAL_SEARCH_ENABLED=true and user toggle), but should be addressed before wider adoption.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions