Skip to content

AI 审计模块

AI 审计是 Observer 阶段的旁路分析模块。它在主请求完成后异步采样、预处理请求/响应内容,并调用外部审计 LLM,结果写入 MongoDB audit_logs

审计不阻塞主链路;队列满或审计失败不会影响用户请求。

配置位置

审计配置位于运行配置:

json
{
  "processors": {
    "audit": {
      "enabled": true,
      "sample_rate": 0.05,
      "worker_count": 2,
      "queue_size": 1024,
      "worker_queue_size": 256,
      "body_limits": {
        "max_request_chars": -1,
        "max_response_chars": -1
      },
      "engines": {
        "llm": {
          "enabled": true,
          "sample_rate": 1.0,
          "url": "https://api.openai.com/v1",
          "api_key": "sk-...",
          "model": "gpt-5.4-mini",
          "max_tokens": 2048,
          "thinking": false,
          "timeout_secs": 60,
          "system_prompt": "你的审计提示词",
          "truncation": {
            "max_request_chars": 20000,
            "max_response_chars": 20000
          },
          "preprocess": {
            "enabled": true,
            "pipelines": {
              "codex": { "enabled": true, "processors": [] },
              "cc": { "enabled": true, "processors": [] },
              "default": { "enabled": true, "processors": [] }
            }
          }
        },
        "rule": {
          "enabled": false,
          "sample_rate": 1.0
        }
      },
      "sink": {
        "batch_size": 20,
        "flush_interval_secs": 5
      }
    }
  }
}

api_key 会加密存储;读取和导出时返回脱敏字段。system_prompt 必须内联保存,当前不支持旧版 @audit_prompt.txt 文件引用。

采样

实际采样率 = processors.audit.sample_rate × engines.llm.sample_rate

采样基于 request_id 做确定性哈希,同一请求在重复处理时结果一致。

预处理管线

审计 LLM 支持按客户端类型分流预处理:

  • codex
  • cc
  • default

每个管线包含:

json
{
  "enabled": true,
  "processors": [
    { "type": "strip_tools", "config": { "mode": "summary", "max_tool_call_input_chars": 512, "max_tool_result_chars": 0 } },
    { "type": "reverse_truncate_messages", "config": { "max_chars": 10000, "max_messages": 20, "mode": "min" } },
    { "type": "format_messages", "config": {} }
  ]
}

支持的预处理器:

类型说明
strip_system_prompt移除系统提示
strip_developer移除 Codex developer 消息
promote_environment_context提取环境上下文
strip_system_reminder移除 Claude Code <system-reminder>
strip_tools精简或移除工具调用与结果
reverse_truncate_messages从尾部保留最近消息
format_messages格式化为审计 LLM 更容易读取的文本

截断

两层截断:

  • body_limits:原始请求/响应进入审计队列前的硬截断。
  • engines.llm.truncation:预处理后、调用审计 LLM 前的截断。

-1 表示不限制,0 表示不发送对应内容。

查看审计结果

在管理端的审计页面查看,或调用管理 API 查询 /api/admin/logs/audit。审计日志包含请求 ID、Key、模型、状态、分析结果、错误原因和审计 LLM token 用量。