we-promise/sure · error · Provider::Anthropic::Error

Too many transactions to auto-detect merchants. Max is 25 pe

Error message

Too many transactions to auto-detect merchants. Max is 25 per request.

What it means

Provider::Anthropic#auto_detect_merchants raises Error when transactions.size > 25. Like auto_categorize, the whole list goes into one prompt with a single forced report_merchants tool call, so the provider caps each request at 25 transactions to bound prompt/response size and keep detection quality stable.

Source

Thrown at app/models/provider/anthropic.rb:102

      result = AutoCategorizer.new(
        client,
        model: effective_model,
        transactions: transactions,
        user_categories: user_categories,
        langfuse_trace: trace,
        family: family
      ).auto_categorize

      upsert_langfuse_trace(trace: trace, output: result.map(&:to_h))

      result
    end
  end

  def auto_detect_merchants(transactions: [], user_merchants: [], model: "", family: nil, json_mode: nil)
    with_provider_response do
      raise Error, "Too many transactions to auto-detect merchants. Max is 25 per request." if transactions.size > 25

      effective_model = model.presence || @default_model

      trace = create_langfuse_trace(
        name: "anthropic.auto_detect_merchants",
        input: { transactions: transactions, user_merchants: user_merchants }
      )

      result = AutoMerchantDetector.new(
        client,
        model: effective_model,
        transactions: transactions,
        user_merchants: user_merchants,
        langfuse_trace: trace,
        family: family
      ).auto_detect_merchants

      upsert_langfuse_trace(trace: trace, output: result.map(&:to_h))

View on GitHub (pinned to e69894adb9)

Solutions

  1. Slice into groups of 25 (each_slice(25) / in_groups_of(25, false)) and call auto_detect_merchants per batch.
  2. Fix the batching in the enqueuing job/service so each enqueued unit is already ≤25 rows.
  3. Reuse the same batching helper used for auto_categorize so both LLM paths share one cap.

Example fix

# before
provider.auto_detect_merchants(transactions: pending)
# => Too many transactions to auto-detect merchants. Max is 25 per request.

# after
pending.each_slice(25) do |batch|
  provider.auto_detect_merchants(transactions: batch, family: family)
end
Defensive patterns

Strategy: validation

Validate before calling

transactions.each_slice(25) do |batch|
  provider.auto_detect_merchants(transactions: batch, family: family)
end

Try / catch

begin
  provider.auto_detect_merchants(transactions: batch, family: family)
rescue Provider::Anthropic::Error => e
  Rails.logger.error("merchant detection failed for batch: #{e.message}")
end

Prevention

When it happens

Trigger: Calling auto_detect_merchants(transactions: txns, ...) with 26+ transactions — typically a merchant-detection job over a large import or a scope that lost its batching during a refactor.

Common situations: Bulk import followed by a merchant-detection sweep with no slicing; concatenating multiple imports into one call; production data volume exceeding what dev/test exercised.

Related errors


AI-assisted analysis of we-promise/sure@e69894adb9 (2026-08-21). Data as JSON: /api/errors/e38720f2725eb669. Report an issue: GitHub.