# Anion gap

Anion gap

## Also searched as

- anion gap
- serum anion gap
- Na Cl HCO3 gap
- anion gap with potassium
- Emmett Narins anion gap
- AG without K
- AG with K

Status: LIVE
Price: $0.005 USDC
`GET /api/calc/anion_gap`
HTML: https://magentlab.com/docs/anion-gap
Markdown: https://magentlab.com/docs/anion-gap.md

Not a medical device. Not a diagnosis, dose, or listing recommendation.

## What it computes

Compute the serum anion gap from sodium, chloride, and bicarbonate, with or without potassium, using the Emmett–Narins 1977 formulas. formula is required.

## When to use

When an agent needs a published anion gap and must not pick with-K vs without-K silently, assign high/low AG, or apply an albumin correction.

## When not to use

- Not a diagnosis. magent does not assign a high or low anion gap.
- Does not correct for albumin. Figge/Fencl albumin-corrected anion gap is not this tool.
- Does not compute a delta gap.

## Formula

```
anion_gap_mmol_l = sodium_mmol_l - chloride_mmol_l - bicarbonate_mmol_l
```

## Citation

[Clinical use of the anion gap](https://doi.org/10.1097/00005792-197701000-00002)
Emmett M, Narins RG
Medicine (Baltimore). 1977;56(1):38-54
DOI: [10.1097/00005792-197701000-00002](https://doi.org/10.1097/00005792-197701000-00002)

## Worked example

### Na 140, Cl 104, HCO3 24 without K

Given: `bicarbonate_mmol_l=24, chloride_mmol_l=104, formula=without_k, sodium_mmol_l=140`

1. anion_gap = 140 - 104 - 24 = 12.0 mmol/L


### Na 140, Cl 104, HCO3 24, K 4.0 with K

Given: `bicarbonate_mmol_l=24, chloride_mmol_l=104, formula=with_k, potassium_mmol_l=4.0, sodium_mmol_l=140`

1. anion_gap = 140 + 4.0 - 104 - 24 = 16.0 mmol/L


## Parameters

- `sodium_mmol_l` (number, required): Serum sodium in mmol/L (110-160).
- `chloride_mmol_l` (number, required): Serum chloride in mmol/L (70-130).
- `bicarbonate_mmol_l` (number, required): Serum bicarbonate in mmol/L (2-45).
- `potassium_mmol_l` (number, optional): Serum potassium in mmol/L (1.5-8.0). Required when formula=with_k. Ignored when formula=without_k.
- `formula` (string, required): without_k (Na - Cl - HCO3) or with_k (Na + K - Cl - HCO3). Required; magent does not pick one silently.

## Bulk (POST)

POST the same path with a JSON items array. Price is n times the GET unit. Invalid items return HTTP 400 before settlement.

Method: `POST /api/calc/anion_gap`
Max items: 25
Price: unit_amount * n (unit 5000 atomic)
Status: PREVIEW

- 1 to 25 items. Each item uses the same keys as the GET query parameters.
- The first invalid item fails the whole request (index in the 400 JSON). You are not charged.
- HTTP 200 is {count, path, items}. Each items[i] is the GET 200 body for that row.
- Catalog misses (empty matches, found=false) stay paid 200s inside that slot.
- x402 resource URL is this path plus n and body_sha256. Browsers that prefer text/html get the same paywall as GET; Pay retries POST with the same items.

Example request:

```json
{
  "items": [
    {
      "bicarbonate_mmol_l": "24",
      "chloride_mmol_l": "104",
      "formula": "without_k",
      "sodium_mmol_l": "140"
    },
    {
      "bicarbonate_mmol_l": "24",
      "chloride_mmol_l": "104",
      "formula": "without_k",
      "sodium_mmol_l": "140"
    }
  ]
}
```

Human paywall (two-item fixture): https://api.magentlab.com/paywall/bulk/anion-gap

Example 200:

```json
{
  "count": 2,
  "path": "/api/calc/anion_gap",
  "items": [
    {
      "disclaimer": "Not a medical device. Deterministic published-formula or published-code output for autonomous agents. A licensed clinician remains responsible for patient care and billing submissions.",
      "citation": {
        "authors": "Emmett M, Narins RG",
        "doi": "10.1097/00005792-197701000-00002",
        "id": "emmett-narins-1977",
        "source": "Medicine (Baltimore). 1977;56(1):38-54",
        "title": "Clinical use of the anion gap"
      },
      "formula": "without_k",
      "formula_expression": "anion_gap_mmol_l = sodium_mmol_l - chloride_mmol_l - bicarbonate_mmol_l",
      "anion_gap_mmol_l": 12.0,
      "bicarbonate_mmol_l": 24.0,
      "chloride_mmol_l": 104.0,
      "sodium_mmol_l": 140.0
    },
    {
      "disclaimer": "Not a medical device. Deterministic published-formula or published-code output for autonomous agents. A licensed clinician remains responsible for patient care and billing submissions.",
      "citation": {
        "authors": "Emmett M, Narins RG",
        "doi": "10.1097/00005792-197701000-00002",
        "id": "emmett-narins-1977",
        "source": "Medicine (Baltimore). 1977;56(1):38-54",
        "title": "Clinical use of the anion gap"
      },
      "formula": "without_k",
      "formula_expression": "anion_gap_mmol_l = sodium_mmol_l - chloride_mmol_l - bicarbonate_mmol_l",
      "anion_gap_mmol_l": 12.0,
      "bicarbonate_mmol_l": 24.0,
      "chloride_mmol_l": 104.0,
      "sodium_mmol_l": 140.0
    }
  ]
}
```

## Response fields

- `formula` (string): without_k or with_k
- `formula_expression` (string): Exact expression used
- `sodium_mmol_l` (number): Input sodium in mmol/L
- `chloride_mmol_l` (number): Input chloride in mmol/L
- `bicarbonate_mmol_l` (number): Input bicarbonate in mmol/L
- `potassium_mmol_l` (number): Serum potassium used when formula=with_k. Omitted when formula=without_k.
- `anion_gap_mmol_l` (number): Anion gap in mmol/L, 1 decimal
- `citation` (object): Emmett and Narins 1977 citation
- `disclaimer` (string): Not-a-device notice

## Example JSON

Frozen fixture. Not a live lookup.

```json
{
  "disclaimer": "Not a medical device. Deterministic published-formula or published-code output for autonomous agents. A licensed clinician remains responsible for patient care and billing submissions.",
  "citation": {
    "authors": "Emmett M, Narins RG",
    "doi": "10.1097/00005792-197701000-00002",
    "id": "emmett-narins-1977",
    "source": "Medicine (Baltimore). 1977;56(1):38-54",
    "title": "Clinical use of the anion gap"
  },
  "formula": "without_k",
  "formula_expression": "anion_gap_mmol_l = sodium_mmol_l - chloride_mmol_l - bicarbonate_mmol_l",
  "anion_gap_mmol_l": 12.0,
  "bicarbonate_mmol_l": 24.0,
  "chloride_mmol_l": 104.0,
  "sodium_mmol_l": 140.0
}
```

## Errors

- HTTP 400 `invalid_sodium`: sodium_mmol_l must be a number from 110 to 160 Returned before settlement; you are not charged.
- HTTP 400 `invalid_chloride`: chloride_mmol_l must be a number from 70 to 130. Returned before settlement; you are not charged.
- HTTP 400 `invalid_bicarbonate`: bicarbonate_mmol_l must be a number from 2 to 45. Returned before settlement; you are not charged.
- HTTP 400 `invalid_potassium`: potassium_mmol_l must be a number from 1.5 to 8.0. Returned before settlement; you are not charged.
- HTTP 400 `invalid_anion_gap_formula`: formula must be without_k or with_k. Returned before settlement; you are not charged.
- HTTP 400 `invalid_items`: POST body must be a JSON object with only an items array Returned before settlement; you are not charged.
- HTTP 400 `invalid_item_count`: items must be an array of 1 to 25 objects Returned before settlement; you are not charged.
- HTTP 400 `invalid_item`: each items entry must be a JSON object Returned before settlement; you are not charged.

Shared HTTP 402 and 503: https://magentlab.com/docs/payments

## Related tools

- [Winters' formula](https://magentlab.com/docs/winters) — Compute expected arterial PCO2 in metabolic acidosis from plasma bicarbonate using the Albert–Winters 1967 formula.
- [Serum osmolality](https://magentlab.com/docs/serum-osmolality) — Compute Worthley calculated serum osmolality from sodium, glucose, and BUN, plus an optional osmolar gap when a measured osm is supplied.

## Payment

Required query parameters must be present and valid. Invalid params return HTTP 400 JSON before settlement. magent does not charge. Fix the params, then request a new 402 quote for that complete URL. Catalog misses (empty matches, found=false) are valid paid 200s.

- GET the tool with Accept: application/json (do not send Accept: text/html unless you want the human paywall).
- HTTP 402 means you have not paid. Decode the base64 PAYMENT-REQUIRED header. HTTP 503 is not a new quote.
- Sign EIP-3009 (scheme exact, assetTransferMethod eip3009) with validBefore = now + accepts[0].maxTimeoutSeconds (600 seconds). That window starts when you sign and must cover wallet confirmation plus CDP verify and settle. Magent retries transient facilitator 429/5xx before answering. Do not send upto or batch-settlement payloads; magent does not offer those schemes.
- Retry the same URL with PAYMENT-SIGNATURE (base64 JSON echoing accepted, payload, and extensions when present).
- Success is HTTP 200 JSON plus PAYMENT-RESPONSE. Settlement happens before the tool runs. HTTP 503 settlement_uncertain or settle_failed: retry the same PAYMENT-SIGNATURE. Mint a new nonce only after a new HTTP 402.

- offered `exact` on eip155:8453: Fixed-price USDC on Base. The buyer authorizes the advertised amount (EIP-3009). magent settles before the tool runs (paymentFlow upfront). extra.assetTransferMethod=eip3009 extra.paymentFlow=upfront.

- not offered `upto`: Not offered. Usage-based: buyer authorizes a ceiling, seller settles the actual amount after work. Reserved for a future metered tool. Not for current calculators or code search.
- not offered `batch-settlement`: Not offered. Payment-channel vouchers claimed later in batches. Not the same as POST {items} bulk (that is still exact).

Same-path POST {items} is still scheme exact: one EIP-3009 authorization for n times the GET unit. That is not x402 batch-settlement (payment channels / vouchers).

Shared HTTP 402 and 503 table: https://magentlab.com/docs/payments

Stdio MCP (one process per host): https://magentlab.com/docs/mcp

## Guarantees

- Invalid query parameters return HTTP 400 before settlement. You are not charged.
- Settlement finishes before the tool executes (paymentFlow upfront). Uncertain or failed settlement fails closed (HTTP 503). Retry the same PAYMENT-SIGNATURE.
- HTTP 402 is unpaid only. Do not sign a second authorization because of a 503.
- Execution is timeout-bounded and concurrency-capped.
- Calculator inputs are stored encrypted for allowlisted operators (Magent Console). They are not in public logs, SSH lookups, catalog, or rollups. Request logs store path without query.
