{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "TokenUsage",
  "description": "Usage for one call. `estimate: None` means source attribution is unavailable\n(for example, a backend assembled the prompt), not zero category usage.",
  "type": "object",
  "properties": {
    "context_window_tokens": {
      "type": "integer",
      "format": "uint64",
      "minimum": 0
    },
    "estimate": {
      "anyOf": [
        {
          "$ref": "#/$defs/RequestEstimate"
        },
        {
          "type": "null"
        }
      ]
    },
    "reported": {
      "anyOf": [
        {
          "$ref": "#/$defs/ReportedUsage"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "context_window_tokens"
  ],
  "$defs": {
    "ByteEstimator": {
      "description": "Explicit heuristic policy. Image transport bytes are never treated as text.\nThis estimate excludes provider framing and is not a tokenizer count.",
      "type": "object",
      "properties": {
        "bytes_per_token": {
          "type": "integer",
          "format": "uint",
          "minimum": 1
        },
        "image_tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        }
      },
      "required": [
        "bytes_per_token",
        "image_tokens"
      ]
    },
    "ContentSource": {
      "description": "The origin category of a piece of model-bound content.",
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "system_prompt"
            }
          },
          "required": [
            "kind"
          ]
        },
        {
          "description": "A typed context block (env details, rules, skills catalog, ...).",
          "type": "object",
          "properties": {
            "block_id": {
              "type": "string"
            },
            "kind": {
              "type": "string",
              "const": "context_block"
            }
          },
          "required": [
            "kind",
            "block_id"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "tool_definitions"
            }
          },
          "required": [
            "kind"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "user_message"
            }
          },
          "required": [
            "kind"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "session_message"
            }
          },
          "required": [
            "kind"
          ]
        },
        {
          "description": "Prior assistant output replayed as history.",
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "assistant_history"
            }
          },
          "required": [
            "kind"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "tool_output"
            },
            "tool_name": {
              "type": "string"
            }
          },
          "required": [
            "kind",
            "tool_name"
          ]
        },
        {
          "description": "Harness-injected content, distinct from user-authored messages.",
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "system_reminder"
            },
            "origin": {
              "type": "string"
            }
          },
          "required": [
            "kind",
            "origin"
          ]
        },
        {
          "type": "object",
          "properties": {
            "hook_name": {
              "type": "string"
            },
            "kind": {
              "type": "string",
              "const": "hook"
            }
          },
          "required": [
            "kind",
            "hook_name"
          ]
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "compaction_summary"
            }
          },
          "required": [
            "kind"
          ]
        }
      ]
    },
    "ReportedUsage": {
      "description": "Provider-reported usage for one model call. Input/cache lanes are disjoint;\nreasoning is a subset of output and must not be added to the output total.",
      "type": "object",
      "properties": {
        "cache_read_tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        },
        "cache_write_tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        },
        "input_tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        },
        "output_tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        },
        "reasoning_tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        }
      },
      "required": [
        "input_tokens",
        "output_tokens",
        "cache_read_tokens",
        "cache_write_tokens",
        "reasoning_tokens"
      ]
    },
    "RequestEstimate": {
      "description": "Categorized estimate. Total derives from categories instead of being a\nseparately mutable field that could drift from them.",
      "type": "object",
      "properties": {
        "method": {
          "$ref": "#/$defs/ByteEstimator"
        },
        "sources": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/SourceEstimate"
          }
        }
      },
      "required": [
        "method",
        "sources"
      ]
    },
    "SourceEstimate": {
      "description": "One source's heuristic allocation, not provider-reported category usage.",
      "type": "object",
      "properties": {
        "images": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        },
        "source": {
          "$ref": "#/$defs/ContentSource"
        },
        "text_bytes": {
          "type": "integer",
          "format": "uint",
          "minimum": 0
        },
        "tokens": {
          "type": "integer",
          "format": "uint64",
          "minimum": 0
        }
      },
      "required": [
        "source",
        "text_bytes",
        "images",
        "tokens"
      ]
    }
  }
}
