Skill / Agent Architecture
1 / 13

Skill / Tool / Agent / Model / Inference 全流程

Discovery → Loading → Context → Model Serving → Structured Output → Agent Runtime → Tool → Verification
1

用户请求

  • 自然语言输入
  • 文件 / 资源 / 当前状态
2

Capability / Skill Discovery

  • Skill Registry
  • Tool Registry
  • MCP / API
  • Rule + Retrieval + LLM Rerank
3

Skill Loading

  • Metadata 先可见
  • 触发后读取 SKILL.md
  • 按需读取 references / scripts / templates
4

Build Main Context

  • system
  • conversation
  • state
  • skills
  • tool schemas
  • retrieved docs
5

Model API

  • messages
  • tools
  • schema / response format
6

Model Serving

  • Tokenize
  • Forward Pass
  • Logits
  • Sampling
  • Constrained Decoding
7

Parser / SDK

  • Text / Tool Call / Structured Output
  • 解析成 Action
8

Runtime Validation

  • Schema
  • Business Rules
  • Permission
  • Approval
9

Branch Execution

  • load_skill
  • tool_call
  • ask_user
  • message
  • finish
10

Verification

  • 结果验证
  • 失败则 repair / retry
11

Final Output

  • 答案
  • 文件
  • 报告
  • PPT / Summary
DiscoveryLoading / ContextModel Serving / InferenceAgent RuntimeTools / External Systems
Model = Intelligence
+
Inference Engine = Generation
+
Agent Runtime = Execution

1. Skill System:Skill 到底由什么组成

Level 0 — Skill Registry
financial-analysis

财务分析

research

证据型研究

pptx

演示文稿生成

pdf

PDF 处理

legal-review

法律审阅

code-review

代码审阅

Level 1 — Skill Metadata
nameskill 唯一名称descriptionDiscovery / Routing 的核心信息path完整 Skill 内容位置version版本tags分类 / 检索标签use when何时触发do not use when何时避免误触发
Level 2 — SKILL.md
Goal

任务目标

Workflow

执行步骤 / SOP

Rules

约束 / 禁止事项

Tool Guidance

什么时候、如何使用 Tool

Output Format

输出规范

Failure Handling

失败时怎么办

Level 3 — References / Assets
templates/

模板

examples/

Few-shot / 示例

scripts/

可执行脚本

domain notes

领域知识

checklists

校验表

style guides

风格规范


Discovery 主要看 Metadata
→
Loading 触发后才读取 SKILL.md / References

2. Skill Discovery / Capability Discovery

输入

用户请求
Conversation
State Summary
权限 / 当前环境

候选集合

Skill Registry
Tool Registry
MCP Servers / APIs
Docs / Resources

候选生成

Keyword / Regex
Rule Routing
Embedding Search
Semantic Retrieval

排序 / 过滤

Permission Filter
Policy Filter
LLM Rerank
Top-K Selection

↓

False Negative

该调用却没召回 → Skill 再好也等于不存在

False Positive

不该调用却误召回 → Context 污染 / 成本上升

Routing Policy

description 不只是说明,也是“什么时候路由到这个 Skill”的政策
Query
→
Candidate Generation
→
Ranking
→
Selected Skills / Tools

3. Skill Loading / Context Engineering

L0:Catalog
namedescriptionpath
L1:Main Skill
SKILL.mdworkflowrules
L2:References
examplestemplatesguides
L3:Task Data
filesretrieved docstool results
Context Builder
context = system_instructions + current_user_request + conversation_history + state_summary + loaded_skill_instructions + tool_schemas + retrieved_documents + recent_tool_results + task_specific_constraints
Progressive DisclosureJust-in-time ContextTask-scoped Loading

Loading 目标风险控制手段
给模型正确的信息加载过少补充 references / tool docs / retrieved data
控制 Context Budget加载过多按需、分阶段、摘要、卸载
减少冲突Skill / system / style 指令冲突优先级、scope、阶段切换

4. Agent 调用模型:Model API / Serving Interface

Agent Runtime → Model API
request = { "messages": [...], "tools": [tool_schema_1, tool_schema_2, ...], "response_format": {...}, "metadata": {...} }
返回给 Agent Runtime
Text Message

普通自然语言回答

Tool Call

工具名 + arguments

Structured Output

符合 schema 的结构化对象

Refusal / Error / Stop

拒绝、异常、停止原因

↓
Model API 是接口
≠
Inference Engine
≠
Agent Runtime

5. Model Serving Stack / Inference Engine

① Model Serving / API Layer
request handling · routing · batching scheduler · streaming · response packaging
② Tokenization
文本 → tokens · tool/schema/context 一并编码
③ Transformer Forward Pass
模型权重 + 输入 tokens → hidden states → logits
④ Logits
每个候选 token 的分数 / 概率前身
⑤ Sampling
temperature · top-k · top-p · greedy · optimized decode
⑥ Constrained Decoding
grammar / JSON Schema / tool protocol → mask 非法 token
⑦ KV Cache / Batching / GPU Scheduling
性能与吞吐:KV cache · dynamic batching · parallelism · GPU/TPU
⑧ Output Packaging
text / structured output / tool call / stop reason → SDK / API response
Inference Engine = 负责“把模型运行出来”

6. Structured Output:Schema、Prompt、Constrained Decoding 的区别

Prompt Constraint
“请严格输出 JSON”

只是提高合法输出概率

↓
Soft Constraint

仍可能输出额外文本、缺字段、格式错误

Schema / Structured Output
JSON Schema

字段、类型、enum、required 等

↓
Model Understanding

模型理解“应该填什么”

Constrained Decoding
Decoder Mask

生成到当前位置时,只允许 grammar 合法 token

↓
Syntactic Hard Constraint

非法 token 概率被置零 / 屏蔽


原始 logits: "search" 7.2 "finish" 6.9 "hello" 8.5 "banana" 3.4 schema 允许: "search" | "finish" mask 后: "search" 7.2 "finish" 6.9 "hello" -∞ "banana" -∞
Syntax = Decoder / Runtime 可强制
Semantics = 主要依赖 Model

7. Tool Calling / Action:模型输出如何变成 Runtime 指令

模型服务返回
{ "type": "tool_call", "name": "search_web", "arguments": { "query": "Apple latest earnings" } }
SDK / Parser 转成 Agent 内部 Action
ToolCallAction( tool="search_web", arguments={ "query": "Apple latest earnings" } )
↓
tool_call

执行外部工具

load_skill

动态读取 Skill 内容

ask_user

请求补充信息

message

普通模型消息

finish

进入最终输出阶段

Action = Agent Runtime 对模型输出的结构化解释 / IR

8. Agent Runtime:Execution Loop

Observe

用户输入 / Tool Result / Files / State

→
Build Context

装配 system + skills + state + tools + docs

→
Call Model

请求 Model API / Serving Stack

↓
Parse Action

解析 tool_call / load_skill / finish ...

→
Validate

schema / business / policy / approval

→
Execute / Load / Ask / Finish

执行 Tool、加载 Skill、追问、结束

↓
Update State

保存工具结果、文件、已完成步骤、phase

Retry / Timeout / Error Handling

失败恢复、重试、降级、异常处理

Loop Back

重新 Observe → Build Context → Call Model

Discover → Load → Execute → Observe → Discover → ...

9. Runtime Validation:为什么“结构正确”还不够

层检查什么例子负责者
1. Syntax / Schema字段、类型、必填项amount 必须是 numberDecoder / SDK / Runtime
2. Business Validation参数是否业务上合理退款金额不能超过订单金额Runtime / Business Logic
3. Permission / Policy是否允许执行用户是否有权删除文件Runtime
4. Approval敏感动作是否需要确认发送邮件 / 金钱操作Runtime + Human
5. Execution Guardtimeout / retry / idempotencyAPI 超时后能否安全重试Runtime
Valid JSON
≠
Valid Action
≠
Correct Decision

Structural Reliability

结构 / schema 是否可靠

Reasoning Reliability

模型是否选对 Tool / 参数 / 下一步

Execution Reliability

Runtime 是否安全、稳定地执行

10. Tools / External Systems / MCP

Web Search

公开网页检索

Browser

网站交互

Filesystem

读取 / 写入文件

PDF / PPT / Office

文档处理

Code Executor

代码运行

Database

查询 / 写入数据

Email / Calendar

消息 / 日历动作

MCP Server

暴露 tools / resources / prompts

Internal Systems

CRM / ERP / 内部 API

Calculator

确定性计算


Skill

How to do something
SOP / workflow / knowledge

Tool

Do something
API / function / external action

Runtime

When / whether / how many times
state / retry / permission / validation
Skill → 告诉模型怎么做
Tool → 真正做
Runtime → 负责调度与控制

11. Final Verification / Completion

Content

任务是否完成
核心结论是否覆盖

Consistency

数字 / 结论 / 引用是否一致

Artifact

文件是否存在
格式 / 页面 / 布局是否正确

Policy / Safety

是否满足权限 / 审批 / 输出约束

↓
PASS

进入 Final Answer / File / Report / PPT / Summary

FAIL

Repair / Retry / Re-run Tool / Reload Context

NEED INFO

Ask User → 获取补充信息 → 回到 Build Context

Execution 不是“生成一次就结束”,而是“执行 → 验证 → 修复 → 再验证”

12. 全栈责任边界:到底是谁在负责什么

层主要职责典型内容
Skill方法 / SOP / 知识什么时候用、怎么做、步骤、规则、工具指南、输出格式
Discovery找到能力Registry、Retrieval、Routing、Rerank、Permission Filter
Loading / Context给模型看什么Progressive Disclosure、Just-in-time、Context Budget、Task Scope
LLM理解 / 推理 / 选择判断任务、选 Tool、决定参数内容、生成语言
Inference Engine运行模型 / 生成 tokenforward pass、logits、sampling、KV cache、constrained decoding
Agent Runtime执行 / 调度 / 控制Action 解析、state、workflow、tool execution、retry、approval、validation
Tool / External System真正操作外部世界Search、Browser、Files、DB、Email、MCP、API、Code
Verification保证任务完成结果检查、文件检查、引用检查、修复 / 重试
Agent Quality = f(Model, Skill, Discovery, Context, Tools, Runtime, State, Evaluation)
最终主链:User → Discovery → Loading → Build Context → Model API → Serving / Inference → Structured Output / Tool Call → Parser → Runtime Validation → Branch Execution → Tool / Load Skill / Ask User → Update State → Loop → Final Verification → Final Output