Skip to Content
Baseera Docs

Logging safely

Use redactForLog to keep keys and tokens out of your logs.

A secret that reaches a log is a secret that has leaked. redactForLog takes any value headed to a logger, and replaces anything shaped like a model provider key or a Baseera secret key with [redacted].

import { redactForLog } from "@baseera/core";

try {
  await baseera.kb.upsert(input);
} catch (error) {
  logger.error(redactForLog(error));
}

What it does

It always returns a string.

InputResult
An Error"Name: message", redacted. The stack is not included.
A stringThe string, redacted.
Anything elseJSON.stringify of the value, redacted. If it cannot be serialized, the string [redacted].
redactForLog("Request failed for sk-ant-api03-abcdefghijklmnop");
// "Request failed for [redacted]"

redactForLog({ key: "sk_" + "a".repeat(64), user: "u_1" });
// '{"key":"[redacted]","user":"u_1"}'

redactSecrets(text) is the same for a plain string, and REDACTED is the replacement text.

What it matches

  • Anthropic keys (sk-ant- and at least 8 more characters).
  • OpenRouter keys (sk-or-).
  • OpenAI style keys (sk-, with proj- or svcacct- allowed, and at least 16 characters).
  • Google keys (AIza and at least 20 characters).
  • Baseera secret keys (sk_ and at least 16 hex characters).
  • Bearer tokens (Bearer and at least 16 token characters).

What it does not match

redactForLog is a safety net, not a guarantee. It matches shapes. It does not match a user token (a JWT), a webhook signing secret (whsec_), or a secret of your own. Do not log those at all.

Rules to follow

  • Never log a key, a user token, a webhook secret or a request header that carries one.
  • Never put a key in state, in a URL, in an error message or in a response body.
  • Log the error code of a BaseeraError, not the whole request.
  • Do not log tool inputs or outputs. They can hold your users' data.

The SDK follows the same rules itself: it never logs keys, tokens, or tool inputs and outputs, and the user token is sent only as a header.

Next steps

On this page