---
title: "One model call for a bookkeeping agent run"
method: POST
path: "/accountant/companies/{customerId}/agent/runs/{runId}/model-call"
tags: ["Accountant Portal"]
---

# One model call for a bookkeeping agent run

`POST /accountant/companies/{customerId}/agent/runs/{runId}/model-call`

## Request

- Base URL: `https://api.medianfi.com/api/v1`
- URL: `https://api.medianfi.com/api/v1/accountant/companies/{customerId}/agent/runs/{runId}/model-call`
- Auth: HTTP bearer

Agent key only; a person gets 403 `AGENT_ONLY` and the shared cloud-bookkeeping service token gets 404. The bookkeeping agent's only way to a model: the API sends the agent's instructions, input and output schema to the configured model under a fixed routing policy (zero data retention, an explicit provider allowlist that provider fallback never leaves, no model fallback) and returns the arguments of one forced tool call as `data.output`. The call must belong to an open run of the company (409 `AGENT_RUN_NOT_OPEN`) started today (UTC): a run started before today's 00:00 UTC is refused with 409 `AGENT_RUN_DAY_ENDED`, because the daily budget counts the spend of runs started today; the caller starts a new run. Refused for a disabled (409 `AGENT_DISABLED`) or stopped (409 `AGENT_KILLED`) company and for a patient-data-restricted one (403 `AGENT_PHI_RESTRICTED`). Spend is integer micro-USD, charged to the company's daily agent budget at once (added to the run's spend) and recorded as `agent_decision` usage (`agent_simulation` for a run with trigger `simulation`, which accepts no evidence, write or decision record: 409 `AGENT_RUN_NOT_OPEN`); its spend counts toward no other model cap. When the budget for today is spent the call is refused with 429 `AGENT_BUDGET_EXHAUSTED`. At most four model calls run at once per company; a fifth is refused with 429 `AGENT_MODEL_CALL_IN_FLIGHT` and may be retried when one finishes. A run's finish adds only the spend it reports beyond what this route already charged. No attempt may use the whole deadline (BK-023): each is cut off at `AGENT_MODEL_ATTEMPT_TIMEOUT_MS` (default 55 s) or at what is left of the deadline, and an attempt cut off at its own cap, or an answer with no usable tool call or missing an `output.requiredFields` field, is asked again at once while at least 10 s of the deadline is left. A failed model call answers 503 `AGENT_MODEL_UNAVAILABLE` (503 `AGENT_MODEL_POLICY_VIOLATION` when the provider broke the routing policy), with `error.extensions.failureCode`, `error.extensions.usage` (what the attempt cost) and `error.extensions.finishReason` and `error.extensions.provider` (the last answer's, null when no allowlisted provider answered); the same 503 `AGENT_MODEL_UNAVAILABLE`, without them, when the model is not configured. At a PHI-bounded company (`phi_bounded_ingestion`, read by the API; the body cannot say) the agent makes no model call: the call is refused with 403 `AGENT_PHI_BOUNDED_NO_MODEL` before any provider call and nothing is charged. A failed read of that flag answers 503. Mounted only when the deployment enables the bookkeeping agent (default off); otherwise 404. Writes no books and spends no write budget.

## Path parameters

- `customerId` string, uuid, required
- `runId` string, uuid, required

## Request body

- BookkeepingAgentModelCallInput
  - `instructions` string, required
  - `input` string, required
  - `output` object, required
    - `name` string, required
    - `description` string
    - `schema` object, required: A JSON schema whose `type` is `object`, at most 32,000 bytes as JSON. The model is forced to call one tool with these parameters.
    - `requiredFields` string[]: MI-083: top-level fields the caller cannot use an answer without; each must be a property `schema` declares (else 422). Never sent to the model. An answer that misses one, or carries null or a value its declared property refuses (`type`, `enum`, `minimum`, `maximum`), is asked again at once within the deadline. When no attempt is complete, the last answer is returned as without the field and the caller's own validation decides.
  - `model` 'primary' | 'escalation'
  - `maxOutputTokens` integer

## Response `200`

Success envelope

- object
  - `success` true, required
  - `data` BookkeepingAgentModelCallResult, required
    - `output` object, required: The arguments of the forced tool call, parsed. Untrusted; the agent validates them.
    - `model` string, required
    - `provider` string, required: The provider that answered, normalized.
    - `usage` BookkeepingAgentModelUsage, required
      - `promptTokens` integer, required
      - `completionTokens` integer, required
      - `cachedTokens` integer, required
      - `reasoningTokens` integer, required
      - `costMicros` integer, required: Micro-USD.
      - `outboundAttempts` integer, required
      - `unknownCostAttempts` integer, required
      - `abortedAttempts` integer, required: Attempts the API cut off at its per-attempt cap or the deadline; each is charged at least the hold's estimate.
    - `finishReason` 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'error' | 'other' | 'null', nullable: The answer's finish reason as OpenRouter normalizes it (`length`: the output-token cap was reached); `other` for any other value; null when the provider sent none.
    - `remainingBudgetMicros` integer, required: The company's agent budget left today after this call, in micro-USD.

## Other responses

- `401`: Authentication required
- `403`: Insufficient permissions or not a company member
- `404`: Resource not found
- `409`: Run not open (AGENT_RUN_NOT_OPEN), run started before today's UTC day (AGENT_RUN_DAY_ENDED), or the agent is disabled or stopped for this company
- `422`: Validation failed
- `429`: The company's daily agent model budget is spent (`AGENT_BUDGET_EXHAUSTED`), or four of its model calls are already in flight (`AGENT_MODEL_CALL_IN_FLIGHT`; retry when one finishes)
- `503`: The model call failed, the provider broke the routing policy, the agent's model is not configured, or the PHI-bounded flag could not be read

## Changes

- **2026-11-26** `4a2098becaf9`: 3 info
  - added the new optional request property `output/requiredFields`
  - added the optional property `data/finishReason` to the response with the `200` status
  - added the required property `data/usage/abortedAttempts` to the response with the `200` status
- **2026-10-29** `e1fc804546d3`: 1 info
  - endpoint added

[Operation history](https://skmtc.dev/medianfi/apis/median-api/changes/accountant/companies/:customerId/agent/runs/:runId/model-call/post.md)

---

[API](https://skmtc.dev/medianfi/apis/median-api.md) - [All operations](https://skmtc.dev/medianfi/apis/median-api/llms.txt) - [OpenAPI document](https://skmtc.dev/medianfi/apis/median-api/revisions/4f4c2d34276b?raw)
