Docs

Contexts, Metadata & Inputs

How to shape the context vector, what to record about where it came from, and how to point each decision at its source.

Normalise your contexts

Scale every context vector to unit L2 norm before calling /predict. Of all the things you control from the client side, this has the biggest effect.

LinUCB scores an arm as θ·x + α·√(xᵀA⁻¹x). The exploration term grows with ‖x‖, and the regret analysis assumes ‖x‖ ≤ 1. With unnormalised or heavy-tailed features, rare high-norm rows get huge exploration bonuses that dominate arm selection. The model still converges, only far more slowly, and no error or warning tells you why.

Measured on the UCI shuttle benchmark, with identical code and identical alpha:

Context scalingCumulative regret (T = 10,000)
Per-column standardisation2,026
Unit L2 norm709

That is a 2.9× difference from preprocessing alone. Thompson Sampling is affected the same way, because it samples θ̃ ~ N(θ, α²A⁻¹) in the same geometry. If your features have very different ranges, min-max scale each one into [0, 1] first, then L2-normalise the row.

import numpy as np
context = np.array(raw_features, dtype=float)
context /= np.linalg.norm(context) or 1.0   # guard the all-zeros row

# or, with the SDK
from banditdb import normalize_context
context = normalize_context(raw_features)

Let the SDK build the vector

Name the features when you create the campaign, and predict takes a dict. The SDK puts the values in order, appends a constant 1 (a bias term, so each arm can learn its base rate) and scales the vector to unit length. feature_dim is then len(features) + 1, and you can leave it out.

db.create_campaign("offers", ["a", "b"], features=["age", "is_mobile"])
arm, iid = db.predict("offers", {"age": 0.34, "is_mobile": 1.0})

Campaign metadata

metadata on POST /campaign is a JSON object of up to 64 KB. It is stored with the campaign and returned by GET /campaign/:id and GET /campaigns. The server does not interpret it. You can put anything there: owner, ticket, version. The SDKs, the MCP server and the console read these keys:

KeyHoldsWritten by
featuresThe name of each context position, in order.The SDKs' features=[...] and MCP create_campaign. This is what lets contexts be passed as dicts.
contextHow the SDK builds the vector from features. {"bias": true, "normalize": true} appends a constant 1 and scales to unit length.The SDKs, together with features.
decision_modelThe model that produced the context, when one did, such as a classifier or an LLM scoring the input.You.

Recording the decision model

When the context is the output of another model, for example a typed-question model like Jev answering "is the damage visible?", decision_model records the facts that are the same for every decision. Logged data can then be traced back to the model version that produced it.

"metadata": {
  "features": ["damage_visible", "damage_matches_claim", "customer_distressed", "fraud_signals", "bias"],
  "decision_model": {
    "provider": "typesafe",
    "name": "jev",
    "version": "claims-v3",
    "questions": ["damage_visible", "damage_matches_claim", "customer_distressed", "fraud_signals"]
  }
}
FieldMeaning
provider, name, versionIdentify the model. Bump version with every model change.
questionsWhat the model was asked, using the same names as the features that hold its answers.

Start a new campaign for a new model version. When the model changes, so does the meaning of each context position. Arm estimates learned on the old meaning don't carry over cleanly.

Decision inputs (input_ref)

/predict, /batch_predict and /campaign/:id/interact accept an optional input_ref: a pointer to what the decision was made from, such as a document, a database row or a multimodal manifest. BanditDB writes the pointer to the WAL with the decision and never fetches it. It survives restarts and checkpoints while the reward is pending. Available from 2.3.2.

curl -X POST http://localhost:8080/predict -H "X-Api-Key: $APP_KEY" -H "Content-Type: application/json" \
  -d '{"campaign_id": "claims", "context": [0.93, 0.88, 0.18, 0.05, 1.0],
       "input_ref": {"uri": "postgres://crm/tickets/4412?v=17",
                     "sha256": "9f2c…",
                     "media_type": "application/json"}}'
FieldRequiredMeaning
uriYesAny URI with a scheme (s3://, postgres://, mongodb://, https://…), up to 512 bytes, with no whitespace.
sha256NoHex SHA-256 of the exact bytes the model received, stored lowercase. A later join can then prove the source hasn't changed.
media_typeNoMIME type of what uri points to, up to 255 bytes.

For multimodal inputs (text plus images plus a CRM row), point to a manifest that lists the parts.

Rules

  • Never put credentials in the URI. A URI with user:password@host or a credential query parameter (X-Amz-Signature, sig, token, access_token, api_key, …) is refused with a 400 before anything is scored or logged. The WAL cannot be purged, so a leaked secret would stay there for good. Use the unsigned object URI.
  • Don't put personal data in the URI, for the same reason. Use opaque IDs.
  • Rows change. A database row joined later shows its current state, not what the model saw. Use append-only or versioned rows, put a version in the URI (…/4412?v=17), or copy inputs to immutable storage.
  • In /batch_predict, a bad input_ref fails only its own item.