The personal finance app for everyone (by everyone)
Ruby
class Provider::Anthropic::AutoMerchantDetector
include Provider::Anthropic::Concerns::UsageRecorder
include Provider::Anthropic::Concerns::StructuredOutput
TOOL_NAME = "report_merchants".freeze
attr_reader :client, :model, :transactions, :user_merchants, :langfuse_trace, :family
def initialize(client, model:, transactions: [], user_merchants: [], langfuse_trace: nil, family: nil)
@client = client
@model = model
@transactions = transactions
@user_merchants = user_merchants
@langfuse_trace = langfuse_trace
@family = family
end
def auto_detect_merchants
span = langfuse_trace&.generation(name: "auto_detect_merchants_api_call", model: model, input: {
model: model,
transactions: transactions,
user_merchants: user_merchants
})
response = create_structured_message(
tool_name: TOOL_NAME,
operation: "auto_detect_merchants",
model: model,
max_tokens: max_tokens,
system_: Provider::Anthropic.structured_output_system(instructions, model, TOOL_NAME),
messages: [ { role: "user", content: user_message } ],
tools: [ output_tool ],
tool_choice: Provider::Anthropic.structured_output_tool_choice(model, TOOL_NAME)
)
merchants_data = extract_merchants(response)
result = build_response(merchants_data)
record_usage(model, response.usage, operation: "auto_detect_merchants", metadata: {
transaction_count: transactions.size,
merchant_count: user_merchants.size
})
span&.end(output: result.map(&:to_h), usage: usage_hash(response.usage))
result
rescue => e
span&.end(output: { error: e.message }, level: "ERROR")
record_usage_error(model, operation: "auto_detect_merchants", error: e, metadata: {
transaction_count: transactions.size,
merchant_count: user_merchants.size
})
raise
end
# Public so /settings/ai_prompts can render the built-in text by constructing
# this class with `family: nil`.
def instructions
family&.ai_prompt(:merchant_anthropic) || default_instructions
end
private
AutoDetectedMerchant = Provider::LlmConcept::AutoDetectedMerchant
def max_tokens
ENV.fetch("ANTHROPIC_MAX_TOKENS", 4096).to_i
end
def output_tool
{
name: TOOL_NAME,
description: "Return the detected business name and website URL for each input transaction.",
input_schema: {
type: "object",
properties: {
merchants: {
type: "array",
description: "One detection result per input transaction.",
items: {
type: "object",
properties: {
transaction_id: {
type: "string",
description: "The internal ID of the original transaction",
enum: transactions.map { |t| t[:id] }
},
business_name: {
type: [ "string", "null" ],
description: "Detected business name, or null if uncertain or generic"
},
business_url: {
type: [ "string", "null" ],
description: "Business website without the www. subdomain (e.g., \"amazon.com\"), or null if uncertain"
}
},
required: [ "transaction_id", "business_name", "business_url" ],
additionalProperties: false
}
}
},
required: [ "merchants" ],
additionalProperties: false
}
}
end
def default_instructions
<<~INSTRUCTIONS.strip_heredoc
You are an assistant to a consumer personal finance app. Detect the business name and website URL
for each transaction and return the result via the report_merchants tool.
Follow ALL the rules below:
- One result per transaction, correlated by transaction_id
- Do NOT include the www. subdomain in business_url ("amazon.com", not "www.amazon.com")
- User-provided merchants should only be used when the match is unambiguous
- Favor null over false positives; only return values when 80%+ confident
- NEVER return a name/URL for generic descriptions ("Paycheck", "Local diner", "ATM", "POS DEBIT")
Decision order:
1. Identify from your knowledge of global businesses
2. Otherwise, match against the user-provided merchants
3. Otherwise, return null for both fields
INSTRUCTIONS
end
def user_message
<<~MESSAGE.strip_heredoc
User's known merchants:
```json
#{user_merchants.to_json}
```
Transactions to analyze:
```json
#{transactions.to_json}
```
MESSAGE
end
def extract_merchants(response)
tool_use = Array(response.content).find { |block| structured_block_type(block) == :tool_use }
raise Provider::Anthropic::Error, "Model did not invoke #{TOOL_NAME} (stop_reason: #{response.stop_reason})" unless tool_use
input = structured_block_input(tool_use)
input = JSON.parse(input) if input.is_a?(String)
merchants = input.is_a?(Hash) ? (input["merchants"] || input[:merchants]) : nil
raise Provider::Anthropic::Error, "Tool call missing merchants" unless merchants.is_a?(Array)
merchants
end
def build_response(merchants)
merchants.map do |m|
AutoDetectedMerchant.new(
transaction_id: m["transaction_id"] || m[:transaction_id],
business_name: normalize_merchant_name(m["business_name"] || m[:business_name]),
business_url: normalize_value(m["business_url"] || m[:business_url])
)
end
end
def normalize_value(value)
return nil if value.nil?
str = value.to_s.strip
return nil if str.empty? || str.casecmp("null").zero?
str
end
def normalize_merchant_name(value)
str = normalize_value(value)
return nil unless str
return str if user_merchants.blank?
match = user_merchants.find { |m| m[:name].to_s.casecmp(str).zero? }
match ? match[:name] : str
end
end