const result = await invoiceContract.accept(async (attempt) => {
const response = await anthropic.messages.create({
model: process.env.ANTHROPIC_MODEL ?? "claude-sonnet-4-20250514",
max_tokens: 1024,
system: [
"Extract invoice data. Use the provided tool.",
attempt.instructions,
].join("\n\n"),
messages: [
{ role: "user", content: invoiceText },
...attempt.repairs.map((repair) => ({
role: repair.role as "user" | "assistant",
content: repair.content,
})),
],
tools: [
{
name: "extract_invoice",
description: "Extract invoice totals",
input_schema: {
type: "object",
additionalProperties: false,
properties: {
subtotal: { type: "number" },
tax: { type: "number" },
total: { type: "number" },
currency: { type: "string" },
},
required: ["subtotal", "tax", "total", "currency"],
},
},
],
tool_choice: { type: "tool", name: "extract_invoice" },
});
const toolBlock = response.content.find((block) => block.type === "tool_use");
if (toolBlock?.type !== "tool_use") return null;
return JSON.stringify(toolBlock.input);
});