Skip to content

Extraction agents ​

An extraction agent turns unstructured input (a PDF, a scanned letter, a photo, an email) into JSON that matches a schema you define. Typical uses: invoice and receipt readers, CV parsers, form and letter analyzers.

Configure the agent ​

Create an agent of type extraction and set at least:

SettingPurpose
modela chat model; use one with the vision capability to read images and scanned PDFs
instructionswhat to extract and how; may contain {{variables}} filled from the input
outputSchemaJSON Schema of the result; the output is validated against it
inputSchemaoptional JSON Schema of the request's input; invalid input answers 400 INPUT_INVALID
fileswhich files the agent accepts and how they reach the model (see Files)

Output schema ​

With an outputSchema the model is asked for structured output and the result is validated. output in the run result is the parsed JSON object. If the model's answer does not match, the run fails with MODEL_OUTPUT_INVALID or OUTPUT_SCHEMA_MISMATCH.

json
{
  "type": "object",
  "properties": {
    "invoiceNumber": { "type": "string" },
    "issueDate": { "type": "string", "description": "ISO date, YYYY-MM-DD" },
    "currency": { "type": "string", "description": "ISO 4217 code" },
    "total": { "type": "number" },
    "lineItems": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "description": { "type": "string" },
          "quantity": { "type": "number" },
          "amount": { "type": "number" }
        },
        "required": ["description", "amount"]
      }
    }
  },
  "required": ["invoiceNumber", "total", "currency"]
}

Tips:

  • Use description on properties to tell the model about formats and edge cases.
  • Prefer enum for classifications.
  • Keep fields the document may not contain out of required, or allow null, so the model does not invent values.

Instructions with variables ​

Instructions are a template. {{name}} is replaced with input.name (nested paths such as {{customer.country}} work, objects are inserted as JSON, missing values as an empty string):

text
Extract the invoice data. Dates in ISO format. The customer's language is {{language}};
translate line item descriptions into English.

Input ​

The input you send becomes the model's user message: a single string field (for example { "text": "..." }) is sent as is; any other input is sent as formatted JSON. Files are added to the same message.

Run it ​

Upload the file first (scope files:write), then pass its id in files:

bash
FILE_ID=$(curl -s "$HEVKAR_BASE_URL/v1/files" \
  -H "Authorization: Bearer $HEVKAR_API_KEY" \
  -F "file=@invoice.pdf" | jq -r .id)

curl "$HEVKAR_BASE_URL/v1/agents/invoice-extractor/runs" \
  -H "Authorization: Bearer $HEVKAR_API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"input\": {\"language\": \"de\"}, \"files\": [\"$FILE_ID\"]}"
ts
import { readFile } from 'node:fs/promises';
import { Hevkar } from '@hevkar/sdk';

const hevkar = new Hevkar();
const file = await hevkar.files.upload(await readFile('invoice.pdf'), { name: 'invoice.pdf' });

const result = await hevkar.agents.run('invoice-extractor', {
  input: { language: 'de' },
  files: [file.id],
  metadata: { orderId: 'A-1001' },
});
console.log(result.output); // { invoiceNumber: 'RE-2026-118', total: 249.9, ... }
ts
const form = new FormData();
form.append('file', new Blob([await readFile('invoice.pdf')]), 'invoice.pdf');
const file = await fetch(`${baseUrl}/v1/files`, {
  method: 'POST',
  headers: { Authorization: `Bearer ${apiKey}` },
  body: form,
}).then((r) => r.json());

const result = await fetch(`${baseUrl}/v1/agents/invoice-extractor/runs`, {
  method: 'POST',
  headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
  body: JSON.stringify({ input: { language: 'de' }, files: [file.id] }),
}).then((r) => r.json());

Large documents can take a while: use async: true and a webhook (see Async runs and webhooks), and an Idempotency-Key so retries never extract twice (see Idempotency).

Inspect the result ​

GET /v1/runs/{id} (scope runs:read) returns the run detail, including how each file reached the model:

json
{
  "files": [
    {
      "id": "…",
      "name": "invoice.pdf",
      "mimeType": "application/pdf",
      "size": 48213,
      "pages": 2,
      "strategy": "vision"
    }
  ]
}

Guardrails ​

Every agent screens its input. By default (guardrails.sanitizeInput: true) input that looks like a prompt-injection attempt is rejected with 400 INPUT_REJECTED; set onInjection: "flag" to process it and record a prompt_injection flag on the run instead. Input longer than maxInputChars (default 50 000 characters of text) is rejected with 400 INPUT_TOO_LONG.

Hevkar public API v1