Skip to content
XPNCORE

Documentation

Safety Penalty Response (xpncore_penalty)

How to detect content-safety penalty billing from JSON responses, HTTP headers, and SSE streams for users and resellers.

Whenever an administrator configures Safety Penalty pricing for a model or upstream in the Routing Table, the API Gateway automatically attaches `xpncore_penalty` metadata to JSON replies (and `error.xpncore_penalty` on 400/403 refusals) along with `X-XPNCORE-Penalty-*` HTTP headers. Models without penalty pricing stay completely unchanged.

Users and resellers only need a simple `if (body.xpncore_penalty?.applied)` check (or `X-XPNCORE-Penalty-Applied: true`) to warn their end-users or pass through the penalty charge (`amount` / `extra_amount`) automatically.

Key points

  • Branch directly on `body.xpncore_penalty?.applied` for 200 responses, or `body.error?.xpncore_penalty?.applied` for 400/403 policy refusals.
  • `amount` is the full penalty total; `extra_amount` is the surcharge due after subtracting `sale_amount`.
  • Every response carries `X-XPNCORE-Request-Id` and `X-XPNCORE-Penalty-*` headers for zero-parse proxy/middleware checks.
  • Streaming SSE responses emit a trailing `event: xpncore_penalty` frame whenever penalty billing is triggered.
  • Strict SDKs that strip unknown JSON fields can always reconcile via `GET /v1/requests/{request_id}/penalty` using the same API key.

Detecting penalties with if/else#

On any model with Safety Penalty pricing in the Routing Table, JSON answers carry xpncore_penalty (and error bodies carry error.xpncore_penalty). Resellers can branch directly on applied === true.

POST /v1/chat/completions
# Call a model that has safety penalty pricing configured
curl -i -X POST https://api.xpncore.com/v1/chat/completions \
  -H "Authorization: Bearer $XPNCORE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MODEL_ID_WITH_PENALTY",
    "messages": [{"role": "user", "content": "..."}]
  }'

# Inspect HTTP headers:
#   X-XPNCORE-Request-Id: 019...
#   X-XPNCORE-Penalty-Configured: true
#   X-XPNCORE-Penalty-Applied: true
#   X-XPNCORE-Penalty-Amount: 12500
#   X-XPNCORE-Penalty-Currency: VND
200 OK / 400 Error · xpncore_penalty
{
  "id": "chatcmpl-019...",
  "object": "chat.completion",
  "model": "MODEL_ID_WITH_PENALTY",
  "choices": [...],
  "usage": { "prompt_tokens": 1000, "completion_tokens": 120, "total_tokens": 1120 },
  "xpncore_penalty": {
    "configured": true,
    "applied": true,
    "status": "applied",
    "reason": "upstream_refusal",
    "amount": "12500",
    "extra_amount": "11800",
    "sale_amount": "700",
    "currency": "VND",
    "request_id": "019a1b2c-3d4e-7f5a-8b9c-0d1e2f3a4b5c",
    "warning": "Request triggered safety penalty pricing configured for this model."
  }
}

xpncore_penalty fields#

All monetary values are exact decimal strings (such as "12500") matching /v1/pricing. Internal rule terms, excerpts, and upstream accounts are never exposed.

configuredbooleanRequired

true when the model or served upstream has Safety Penalty pricing configured.

appliedbooleanRequired

Primary if/else flag: true when this request triggered safety penalty billing.

statusstringRequired

none (not penalized), applied (penalty calculated), pending (awaiting stream completion), or refused (blocked pre-dispatch using estimated input tokens).

reasonstring | nullOptional

Generic trigger source: input_rule, omni_moderation, upstream_refusal, or null when not penalized.

amountstring | nullOptional

Full penalty total in currency (decimal string), or null when not applied.

extra_amountstring | nullOptional

Additional fee charged beyond the normal sale charge (max(amount - sale_amount, 0)).

sale_amountstring | nullOptional

Normal wallet sale charge already collected for the request, when applicable.

currencystringRequired

Three-letter currency code (VND, USD, ...).

request_iduuidRequired

Request UUID for reconciliation via GET /v1/requests/{request_id}/penalty.

HTTP response headers#

The gateway also sets normalized headers across JSON replies, SSE streams, and 400/403 refusals so reseller proxies can inspect penalties without parsing bodies:

  • X-XPNCORE-Request-Id: unique request UUID.
  • X-XPNCORE-Penalty-Configured: true or false.
  • X-XPNCORE-Penalty-Applied: true or false.
  • X-XPNCORE-Penalty-Status: none, applied, pending, or refused.
  • X-XPNCORE-Penalty-Amount, X-XPNCORE-Penalty-Extra-Amount, X-XPNCORE-Penalty-Currency, X-XPNCORE-Penalty-Reason: present whenever Applied=true.

Streaming SSE & strict-SDK lookup endpoint#

For stream: true, in addition to initial HTTP headers, the gateway emits a trailing event: xpncore_penalty event when a violation occurs (including mid-stream upstream refusals). If your SDK strips unknown JSON fields, query GET /v1/requests/{request_id}/penalty with the same XPNCORE_API_KEY.

GET /v1/requests/{request_id}/penalty
# Reconcile penalty status after streaming or with strict SDKs
curl "https://api.xpncore.com/v1/requests/$REQUEST_ID/penalty" \
  -H "Authorization: Bearer $XPNCORE_API_KEY"
200 OK · GET /v1/requests/{request_id}/penalty
{
  "request_id": "019a1b2c-3d4e-7f5a-8b9c-0d1e2f3a4b5c",
  "xpncore_penalty": {
    "configured": true,
    "applied": true,
    "status": "applied",
    "reason": "omni_moderation",
    "amount": "12500",
    "extra_amount": "12500",
    "sale_amount": "0",
    "currency": "VND",
    "request_id": "019a1b2c-3d4e-7f5a-8b9c-0d1e2f3a4b5c",
    "warning": "Request triggered safety penalty pricing configured for this model."
  }
}