{"record":{"id":"86eb58e9219c42cc","repo":"we-promise/sure","slug":"pdf-exceeds-anthropic-s-32-mb-limit-pdf-content","errorCode":null,"errorMessage":"PDF exceeds Anthropic's 32 MB limit (#{pdf_content.bytesize} bytes)","messagePattern":"PDF exceeds Anthropic's 32 MB limit \\(#(.+?) bytes\\)","errorType":"exception","errorClass":"Provider::Anthropic::Error","httpStatus":null,"severity":"error","filePath":"app/models/provider/anthropic/bank_statement_extractor.rb","lineNumber":22,"sourceCode":"  TOOL_NAME = \"report_bank_statement\".freeze\n\n  # Mirrors Provider::Anthropic::PdfProcessor::MAX_PDF_BYTES.\n  MAX_PDF_BYTES = 32 * 1024 * 1024\n\n  attr_reader :client, :model, :pdf_content, :langfuse_trace, :family\n\n  def initialize(client:, model:, pdf_content:, langfuse_trace: nil, family: nil)\n    @client = client\n    @model = model\n    @pdf_content = pdf_content\n    @langfuse_trace = langfuse_trace\n    @family = family\n  end\n\n  def extract\n    raise Provider::Anthropic::Error, \"PDF content is required\" if pdf_content.blank?\n    if pdf_content.bytesize > MAX_PDF_BYTES\n      raise Provider::Anthropic::Error,\n            \"PDF exceeds Anthropic's 32 MB limit (#{pdf_content.bytesize} bytes)\"\n    end\n\n    span = langfuse_trace&.span(name: \"extract_bank_statement_api_call\", input: {\n      model: model,\n      pdf_size: pdf_content.bytesize\n    })\n\n    response = client.messages.create(\n      model: model,\n      max_tokens: max_tokens,\n      system_: instructions,\n      messages: [ { role: \"user\", content: user_content } ],\n      tools: [ output_tool ],\n      tool_choice: { type: \"tool\", name: TOOL_NAME, disable_parallel_tool_use: true }\n    )\n\n    parsed = extract_tool_input(response)","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/we-promise/sure/blob/e69894adb92547273377398c15f45c979cd9416a/app/models/provider/anthropic/bank_statement_extractor.rb#L4-L40","documentation":"BankStatementExtractor#extract raises when pdf_content.bytesize exceeds MAX_PDF_BYTES (32 MiB). Anthropic's Messages API rejects requests larger than 32 MB total, and this extractor sends the whole statement as one native document block, so oversized PDFs are stopped locally with a clear message before wasting an API round-trip.","triggerScenarios":"Uploading a very large bank statement PDF (>32 MiB — typically multi-year exports or scan-heavy statements with embedded images) through statement import; the guard fires before client.messages.create.","commonSituations":"Users exporting years of history from their bank; scanned (image-based) PDFs bloated to tens of MB; print-to-PDF with full-resolution page images; the app's own upload limit being higher than the model's request limit.","solutions":["Split the statement into smaller PDFs (per year/month) or export a shorter date range from the bank, then import each part.","Compress/re-save the PDF (downsample scanned images, e.g. via ghostscript/qpdf) to get under 32 MiB.","If you control the upload UI, enforce a client- and server-side size cap below 32 MiB with a friendly message so users never reach this raise."],"exampleFix":"# before\nstatement.import! # 45 MB scanned PDF passes upload, dies at extract\n# => PDF exceeds Anthropic's 32 MB limit (47185920 bytes)\n\n# after\nif pdf.bytesize > 32.megabytes\n  render_error(\"Statement is too large (#{pdf.bytesize / 1.megabyte} MB). Please export a shorter date range or compress the PDF.\")\nelse\n  statement.import!\nend","handlingStrategy":"validation","validationCode":"MAX = 32.megabytes\nif pdf.bytesize > MAX\n  return error(\"Statement is #{pdf.bytesize / 1.megabyte} MB; limit is 32 MB. Split or compress it.\")\nend\nextractor.extract","typeGuard":null,"tryCatchPattern":"begin\n  extractor.extract\nrescue Provider::Anthropic::Error => e\n  statement.mark_import_failed(reason: e.message) # user-actionable message included\nend","preventionTips":["Enforce a <32 MiB cap in the upload UI (client + server) with a friendly message.","Suggest shorter export ranges for multi-year statements in the import docs.","Pre-compress scanned PDFs before import."],"tags":["anthropic","llm","pdf","file-size-limit","bank-statement"],"backgroundTag":"payload-too-large","analyzedSha":"e69894adb92547273377398c15f45c979cd9416a","analyzedAt":"2026-08-21T18:22:41.165Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}