跳转到内容
EN

读取会话上下文(扩展)

GET/agent/v1/sessions/{session_id}/context● 支持

请求参数

  • session_idstring必填

    path: 会话 ID(sesn_...),由 sessions.create 或 sessions.list 返回。资源和线程调用的 session_id 查询参数也接受同一 ID,且必须与路径值一致。

返回

200

扩展:仅在 /agent/v1 提供,形状参照 Claude Agent SDK getContextUsage(),字段使用 snake_case。detail 固定为 summary:总量优先使用上次模型实测,各来源按本地估算占比摊分,used 分类之和等于 total_tokens。categories 只列非零来源,按 kind(used/free/buffer/deferred)判断用途。key 是稳定来源标识扩展;客户端须容忍未知 key 和新增来源,未知名称回退显示 name。MCP 按服务器归组,插件按 id 归组,摘要单独计量。excluded_items 暂为零。尚无测量时 total_tokens、total_tokens_source、measured_at 为 null,categories 为空,明细为零或空数组。切换模型后 max_tokens、auto_compact_threshold 与 free/buffer 立即按新模型重算,保留最近测量值。窗口未知或自动压缩熔断时 is_auto_compact_enabled 为 false。例如 model.thinking 为 {efforts: [high, xhigh], can_disable: true, default_effort: high}。model.thinking 返回 efforts(支持的档位)、can_disable(是否允许 none)及 default_effort(模型默认档位,可为 null);能力未知时整个 thinking 为 null。会话不存在或调用者不可见时返回 404。

错误按 Anthropic 错误格式返回(type / message / request_id),参见 错误码

请求示例

curl
curl https://api.deepraxis.com/agent/v1/sessions/sesn_abc123/context \
  -H "Authorization: Bearer $PRAXIS_API_KEY"
Python
import requests

opts = dict(
    base_url="https://api.deepraxis.com/agent",
    api_key="$PRAXIS_API_KEY",
)

# /agent 是我们自己的 Managed Agents 形状,直接发 HTTP 请求(Bearer 认证)
resp = requests.get(
    f"{opts['base_url']}/v1/sessions/sesn_abc123/context",
    headers={"Authorization": f"Bearer {opts['api_key']}"},
)
resp.raise_for_status()
context = resp.json()
print(context["total_tokens"], context["max_tokens"])
for category in context["categories"]:
    if category["kind"] == "used":
        print(category["name"], category["tokens"])
TypeScript
const opts = {
  baseURL: "https://api.deepraxis.com/agent",
  apiKey: "$PRAXIS_API_KEY",
};

// /agent 是我们自己的 Managed Agents 形状,直接发 HTTP 请求(Bearer 认证)
const resp = await fetch(`${opts.baseURL}/v1/sessions/sesn_abc123/context`, {
  headers: { Authorization: `Bearer ${opts.apiKey}` },
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
const context = await resp.json();
console.log(context.total_tokens, context.max_tokens);
for (const category of context.categories) {
  if (category.kind === "used") console.log(category.name, category.tokens);
}

响应示例 200

{
  "type": "session_context",
  "session_id": "sesn_b06147c12f7b4a9380588063e2e7090e",
  "model": {
    "id": "deepseek-v4-flash",
    "display_name": "deepseek-v4-flash"
  },
  "total_tokens": 12000,
  "total_tokens_source": "actual",
  "max_tokens": 200000,
  "max_input_tokens": null,
  "percentage": 6,
  "auto_compact_threshold": 178808,
  "is_auto_compact_enabled": true,
  "clear_tool_results_at": 100000,
  "detail": "summary",
  "measured_at": "2026-10-03T14:56:41.756Z",
  "categories": [
    {
      "key": "system",
      "name": "System prompt",
      "tokens": 1313,
      "kind": "used"
    },
    {
      "key": "tools",
      "name": "System tools",
      "tokens": 2250,
      "kind": "used"
    },
    {
      "key": "mcp:github",
      "name": "MCP tools · github",
      "tokens": 2625,
      "kind": "used"
    },
    {
      "key": "plugin:environment",
      "name": "environment",
      "tokens": 938,
      "kind": "used"
    },
    {
      "key": "messages",
      "name": "Messages",
      "tokens": 2437,
      "kind": "used"
    },
    {
      "key": "summary",
      "name": "Compaction summary",
      "tokens": 2437,
      "kind": "used"
    },
    {
      "key": "free",
      "name": "Free space",
      "tokens": 166808,
      "kind": "free"
    },
    {
      "key": "buffer",
      "name": "Autocompact buffer",
      "tokens": 21192,
      "kind": "buffer"
    }
  ],
  "mcp_tools": [
    {
      "name": "search_issues",
      "server_name": "github",
      "tokens": 2625
    }
  ],
  "plugin_blocks": [
    {
      "name": "environment",
      "tokens": 938
    }
  ],
  "message_breakdown": {
    "user_message_tokens": 2437,
    "assistant_message_tokens": 0,
    "tool_call_tokens": 0,
    "tool_result_tokens": 0,
    "reasoning_tokens": 0,
    "tool_calls_by_type": []
  },
  "compaction_summary": {
    "tokens": 2437
  },
  "excluded_items": {
    "count": 0,
    "tokens": 0
  }
}

响应

HTTP 200

Current model and latest context measurement

application/json

名称 类型 说明
type string Always session_context. Snapshot of a session's context window usage.
session_id string Session whose context this snapshot describes.
model object Model of the session and its context window facts.
model.id string Identifier of the object, as returned by the API; pass it in the path of follow-up calls on the same resource.
model.display_name string Human-readable name of the model.
model.thinking object | null Thinking capability of the model: supported efforts, whether thinking can be disabled, and the default. Null when the capability is unknown.
total_tokens number | null Tokens currently occupying the context window; null before the first measurement.
total_tokens_source string | null Whether total_tokens is the provider-reported input usage (actual) or a local estimate (estimated); null before the first measurement.
max_tokens number | null Context window of the model in tokens; null when the model does not state one.
max_input_tokens number | null Maximum input tokens the model accepts per request; null when not stated.
percentage number Share of the context window in use, as a whole-number percent; 0 when the window or usage is unknown.
auto_compact_threshold number | null Token count at which automatic compaction starts; null when the window is unknown.
is_auto_compact_enabled boolean Whether automatic compaction is enabled for this session.
clear_tool_results_at number Token count at which old tool results are cleared from the context.
detail string Level of detail of the snapshot.
categories array<object> Context usage by category; each row has a stable key and a kind.
categories[].key string Praxis extension: stable source identifier (system, tools, mcp:<server>, plugin:<id>, messages, summary, free, buffer). Clients must tolerate unknown keys and newly added sources; classify rows by kind, not key or name.
categories[].name string Display name of the category.
categories[].tokens number Tokens used by the category.
categories[].kind string How the category counts against the window.
mcp_tools array<object> Tokens spent on the tool definitions of each MCP server.
mcp_tools[].name string Name of the MCP tool.
mcp_tools[].server_name string Name of the MCP server that provides the tool.
mcp_tools[].tokens number Tokens used by the tool definition.
plugin_blocks array<object> Tokens spent on context blocks injected by each mod.
plugin_blocks[].name string Name of the mod that injected the block.
plugin_blocks[].tokens number Tokens used by the block.
message_breakdown object Tokens in the conversation messages, split by role and tool traffic.
message_breakdown.user_message_tokens number Tokens of user messages.
message_breakdown.assistant_message_tokens number Tokens of assistant text messages.
message_breakdown.tool_call_tokens number Tokens of tool-call arguments.
message_breakdown.tool_result_tokens number Tokens of tool results.
message_breakdown.reasoning_tokens number Tokens of reasoning content.
message_breakdown.tool_calls_by_type array<object> Tool-call and tool-result tokens grouped by tool name.
compaction_summary object | null Tokens held by the latest compaction summary; null when the session was never compacted.
compaction_summary.tokens number Tokens of the compaction summary.
excluded_items object Items removed from the context for content safety, kept for audit only and not counted as usage.
excluded_items.count number Number of excluded items.
excluded_items.tokens number Tokens of the excluded items.
measured_at string | null RFC 3339 time of the measurement; null before the first measurement.