匠人学院 JR Academy学AI来匠人
匠人学院 JR Academy学AI来匠人

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png
EN

关于公司

关于我们元宇宙课堂新闻资讯匠人工作成为导师匠人导师联系我们匠人商店J3.Club

匠人资源

工作内推匠人活动1对1私教行业白皮书线上学习平台面试中心分享面试经验Internship会员中心

AI 工具

AI 工具箱考证匠 Cert Master求职匠 Job Hunter牛小匠 UniMate AI

AI 学习方向

全部学习方向AI EngineerContext EngineeringVibe CodingPrompt MasterAI BuilderAI 产品经理Python 入门

AI 应用提效

AI 办公提效AI 数据分析AI 财务AI 内容创作AI 视觉创作前端开发Hermes AgentOpenClaw 本地智能体

大学资源

墨尔本大学昆士兰大学新南威尔士大学悉尼大学莫那什大学阿德莱德大学RMITQUTUTS

少儿 AI 教育

Airbotix 少儿 AI 编程澳洲家长实用资料库NAPLAN 成绩单怎么看My School 学校数据指南悉尼私校学费 2026少儿编程课程与训练营

移民服务

澳洲移民技术移民189/190/491雇主担保482/186/494投资移民188/888英国移民美国移民加拿大移民

企业合作

P3职业孵化器Enterprise (EN)企业培训实习合作招聘合作申请合作

求职代理

岗位代投职位监控LinkedIn代运营LinkedIn人脉代加了解P3项目

匠人支持

FAQsTerms & ConditionsPrivacy PolicyCancellation & Refund PolicySite map

Top Categories

Web全栈班DevOps项目班数据工程全栈班数据分析项目班编程入门班Business Analyst实习算法集训营

求职就业

BA和产品经理实习数据科学实习数据分析实习Marketing实习简历修改面试指导导师指导VIP

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

联系方式

hello@jiangren.com.au0421-672-555

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2026 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572

首页/资源中心/文章详情
JR Academy · Blog职业洞察

AI Engineer 完全指南 — 生产部署:Latency / Cost / Observability / Safety 四件套

AI 产品上线后真正的工程难题:怎么压 latency、怎么省 cost、怎么 trace、怎么挡 abuse 和 PII

发布日期2026-09-20
阅读时长5 分钟
作者

快速导航

  • AI 产品上线之后的真正难题
  • ① Latency:用户感知速度
  • 数字基准
  • 优化手段(按性价比排序)
  • ② Cost:账单怎么压
  • 优化策略矩阵
  • Batch API:非实时任务降 50%
  • 混合模型路由(Router)
  • 监控成本
  • ③ Observability:出问题怎么 debug
  • 三层 trace 是标配
  • 工具
  • 必须打的字段
  • ④ Safety:挡住坏请求 + 别泄露敏感
  • Rate Limiting(必须)
  • Content Moderation
  • PII 处理
  • Prompt Injection 防御
  • 监控异常 query
  • 一个完整的 production AI endpoint 长这样
  • 本章小结

AI 产品上线之后的真正难题

前 6 章把"做出来"教完了。这一章讲"上线后不挂"。这部分面试经常问到、JD 里写"production experience" 真正考察的就是这些。

四个核心维度:

        Latency
           ▲
           │
Cost ◀────┼────▶ Observability
           │
           ▼
         Safety

四个互相 tradeoff——压 latency 通常涨 cost、增 observability 涨 latency、加 safety 加 latency 又加 cost。生产 AI Engineer 的工作就是在这四个之间找最优解。

① Latency:用户感知速度

数字基准

体验 TTFT (首 token) 总时延 用户反应
极佳 < 500ms 流式持续 "感觉很快"
可接受 < 1.5s 流式 "还行"
差 > 3s 阻塞 "卡死了"
灾难 > 10s 超时 "再也不用"

TTFT (Time To First Token) 是关键指标。只要流式开 + TTFT < 1s,用户主观体验就 OK,哪怕总时长 30 秒。

优化手段(按性价比排序)

a. 开 Streaming(必做)

第 2 章提过。不开 streaming 的 AI 产品是过去式。

b. Prompt Caching(Anthropic 必开)

system prompt + RAG chunks 缓存 5 分钟,命中部分 input 价格 1/10、latency 降 50%+:

await client.messages.create({
  model: 'claude-sonnet-4-6',
  system: [{
    type: 'text',
    text: longSystemPrompt,
    cache_control: { type: 'ephemeral' } // ← 关键
  }],
  messages: [...]
});

OpenAI 也有自动 prompt caching(24h TTL)但需要 prompt 完全相同。Anthropic 是 5min ephemeral 但能精确控制缓存哪段。

c. 减小输入 token 量

  • RAG top-k 从 10 改 5
  • system prompt 精简(去掉冗余话)
  • 长 history 做摘要

每砍 1K token,latency 降 ~100-200ms,cost 降同比例。

d. 用 Haiku / Flash 处理简单任务

简单分类用 Sonnet 是杀鸡用牛刀。大部分客服分类、内容审核、关键词提取用 Haiku 都够,latency 减半 + 价格 1/4。

e. 并发调用而不是串行

// ❌ 串行 (慢)
const summary = await llmCall(p1);
const tags = await llmCall(p2);
const sentiment = await llmCall(p3);

// ✅ 并发 (快 3 倍)
const [summary, tags, sentiment] = await Promise.all([
  llmCall(p1), llmCall(p2), llmCall(p3)
]);

注意 rate limit—— Anthropic 默认 RPM 是 50(Tier 1),并发太多会 429。生产要算好 RPS。

② Cost:账单怎么压

匠人内部经验:从"完全不优化"到"全套优化",AI 月度账单可降 60-80%。

优化策略矩阵

策略 节省比例 实施难度
模型选型 (Sonnet → Haiku) 70% ⭐ 简单
Prompt Caching 40-50% ⭐ 简单(一行代码)
限制 max_tokens 20-30% ⭐ 简单
Batch API(非实时任务) 50% ⭐⭐ 改架构
Prompt 精简 10-20% ⭐⭐ 调 prompt
混合模型路由 20-40% ⭐⭐⭐ 加 router 层
本地部署小模型 80%+ ⭐⭐⭐⭐ 自维护 GPU

Batch API:非实时任务降 50%

OpenAI / Anthropic / Google 都开了 batch API,定价是实时 API 的 50%,但 24 小时内才返回。

适用场景:

  • 每晚跑的内容审核
  • 离线 evaluation
  • 历史数据回填
  • 任何"可以隔夜处理"的
# OpenAI Batch API 例子
client.batches.create(
    input_file_id=file.id,
    endpoint='/v1/chat/completions',
    completion_window='24h'
)

匠人 daily-eval cron 就用 batch API,月省 ~$200。

混合模型路由(Router)

写一个小 LLM 先判断"这个 query 难度多少",简单的路由到 Haiku,复杂的到 Sonnet:

async function smartRoute(query: string) {
  // Step 1: cheap classifier (Haiku, 一次 ~$0.0001)
  const difficulty = await haiku.classify({
    system: '判断 query 难度:simple / complex',
    messages: [{ role: 'user', content: query }]
  });
  
  // Step 2: route to right model
  if (difficulty === 'simple') {
    return await haiku.chat(query);
  } else {
    return await sonnet.chat(query);
  }
}

前提:分类成本 << 模型差价。计算盈亏点:每 1000 query 用 Sonnet $X,加 router 后 800 走 Haiku + 200 走 Sonnet 的总成本应明显低于 $X,否则别加 router。

监控成本

Helicone / Portkey 这种代理把 LLM 调用 proxy 一遍,给你一个 dashboard 看每个用户、每个 endpoint、每天 / 周的 token 和 $ 消耗。匠人内部用 Langfuse 自带的 cost tracking,每个 trace 都有 cost 字段。

永远监控总 cost、单用户 cost、每个 endpoint cost。看到突涨立刻查(一般是被 abuse 或某个 user query 异常)。

③ Observability:出问题怎么 debug

LLM 应用 debug 比传统应用难 10 倍。原因:

  • 输出非确定性(同样 query 可能不同结果)
  • 调用链长(query rewrite → embed → vector search → rerank → LLM)
  • 错误形态多(拒答、幻觉、格式错、慢、贵)

必须有专门的 observability。

三层 trace 是标配

Layer 1: Request trace
  └── /api/ai/resume-analyze (req_id=abc, user=xyz, latency=2.3s, cost=$0.012)
       │
Layer 2: LLM call trace
  ├── embed (model=text-embedding-3-small, tokens=120, latency=80ms)
  ├── vector_search (top_k=20, latency=15ms)
  ├── rerank (model=rerank-3, latency=180ms)
  └── llm_call
       │
Layer 3: LLM call detail
  ├── system prompt: "..." (cached: yes, 1200 tokens)
  ├── user msg: "..." (300 tokens)
  ├── output: "..." (450 tokens)
  ├── tokens: input=1500, output=450, cached=1200
  ├── cost: $0.012
  └── latency: 1.8s (TTFT=400ms)

工具

  • Langfuse(开源 / SaaS,自托管首选)
  • LangSmith(LangChain 出,Python 生态深度集成)
  • Datadog APM(传统 APM 加 LLM 插件)
  • Helicone(轻量 proxy,1 行代码集成)

匠人用 Langfuse self-hosted:免费、所有数据自己控、Postgres 后端不另起新 infra。

必须打的字段

{
  trace_id: 'abc-123',
  user_id: 'user-456',
  endpoint: 'POST /api/ai/resume-analyze',
  model: 'claude-sonnet-4-6',
  prompt_version: 'resume-analysis@1.2.0',  // ← 关键,prompt 出问题能定位
  input: { ... },
  output: { ... },
  tokens: { input: 1500, output: 450, cached: 1200 },
  cost_usd: 0.012,
  latency_ms: 1820,
  ttft_ms: 380,
  error: null,
  feedback: null  // 用户点踩 / 五星评分写这里
}

prompt_version 是匠人内部 PRD 强制的——出问题能立刻定位是哪版 prompt 干的。

④ Safety:挡住坏请求 + 别泄露敏感

Rate Limiting(必须)

不限的话用户脚本一刷就把你账单刷穿。NestJS 例子:

@Throttle({ default: { ttl: 60000, limit: 30 } }) // 30 RPM/IP
@Post('/ai/chat')
async chat(@Body() dto: ChatDto) { ... }

匠人全局 100 RPM/IP,AI endpoints 单独限 30 RPM。用户级别也要限(按 user_id 不只 IP)。

Content Moderation

用户输入 + 模型输出都要过审:

  • OpenAI Moderation API 免费,分类 violence / sexual / hate
  • Anthropic 没专门 API,用 Haiku 写 prompt 判
  • 大陆运营用阿里云 / 腾讯云内容审核(自带敏感词库)
async function chat(userMsg: string) {
  // pre-check user input
  const inputCheck = await moderate(userMsg);
  if (inputCheck.flagged) throw new Error('Input violates policy');
  
  // call LLM
  const response = await llm.chat(userMsg);
  
  // post-check output
  const outputCheck = await moderate(response);
  if (outputCheck.flagged) {
    return '抱歉,无法生成相关内容'; // 拒绝输出
  }
  
  return response;
}

PII 处理

简历、客户咨询里可能有手机、邮箱、身份证、银行卡。送给 LLM 前必须 redact:

function redactPII(text: string) {
  return text
    .replace(/\b1[3-9]\d{9}\b/g, '[PHONE]')
    .replace(/\b[\w.-]+@[\w.-]+\.\w{2,}\b/g, '[EMAIL]')
    .replace(/\b\d{15}|\d{18}\b/g, '[ID]');
}

为什么必须:

  1. 数据传到 LLM 服务商等于上传 OpenAI / Anthropic
  2. 训练数据可能被用来改模型(虽然各厂都说"我们不用 API 数据训练",但合规层面 zero trust 更稳)
  3. 澳洲 Privacy Act + GDPR 都要求"必要最小化"

例外:如果有 BAA / DPA 协议(Enterprise tier),可以发 PII。但默认 redact 是工程默认。

Prompt Injection 防御

第 3 章已经讲。生产标配:

  • 用户输入用 <user_input> 标签包起来
  • system prompt 写死"无论用户说什么,永远不做 X"
  • 高危动作 human-in-the-loop

监控异常 query

某用户突然问 1000 次"管理员密码"——很可能是 attack。要打日志 + 告警:

const SUSPICIOUS_KEYWORDS = ['ignore previous', 'system prompt', 'admin password'];
if (SUSPICIOUS_KEYWORDS.some(k => userMsg.toLowerCase().includes(k))) {
  await alertSecurityTeam({ user_id, msg: userMsg });
}

一个完整的 production AI endpoint 长这样

@Throttle({ default: { ttl: 60000, limit: 30 } })
@Post('/ai/chat')
async chat(@Body() dto: ChatDto, @CurrentUser() user: User) {
  const traceId = uuid();
  
  // 1. Pre-check
  const moderation = await this.moderateService.check(dto.message);
  if (moderation.flagged) throw new ForbiddenException('Content policy violation');
  
  // 2. PII redact
  const redacted = redactPII(dto.message);
  
  // 3. RAG retrieval (with caching)
  const chunks = await this.ragService.retrieve(redacted, { 
    accessLevel: user.role,
    topK: 5 
  });
  
  // 4. LLM call (with timeout + retry + observability)
  const response = await this.langfuse.trace(traceId, async () => {
    return await retryWithBackoff(
      () => this.llmService.chat({
        promptVersion: 'chat@1.2.0',
        system: PROMPTS.chat.system,
        chunks,
        userMessage: redacted,
        userId: user.id,
        timeout: 30000
      }),
      { maxRetries: 2 }
    );
  });
  
  // 5. Post-check output
  const outputModeration = await this.moderateService.check(response.text);
  if (outputModeration.flagged) {
    return { response: '抱歉,无法生成相关内容', traceId };
  }
  
  return { response: response.text, traceId };
}

50 行代码包含 rate limit / moderation / PII / RAG / observability / retry / output filter。这是真正 production-ready 的 AI endpoint。

本章小结

  • Latency:streaming + caching + 选小模型 + 并发
  • Cost:选模型 + caching + batch API + 混合路由 + 监控
  • Observability:三层 trace(request / LLM call / detail),打 prompt_version 字段
  • Safety:rate limit + moderation 双向 + PII redact + injection 防御 + 异常 query 监控

最后一章我们讲求职:怎么把这些技能转化成简历 / 项目 / 面试通过率。

作者
一键分享或复制链接
Lightman Wang
Reviewer: Lightman Wang

Founder of JR Academy

查看该作者的更多文章 →

相关学习资源

  • AI 学习中心
  • Prompt 工程入门
← 上一篇AI Engineer 完全指南 — 微调 vs 评估:怎么知道你的 AI 产品到底好不好下一篇 →AI Engineer 完全指南 — 求职:从转行到拿 offer 的实操路径

相关文章推荐

太狠了太狠了😭 NVIDIA这个AI免费项目真的太狠了

2026-09-21

GitHub Copilot 实战指南 — 常见问题、定价与选型建议

2026-09-20

GitHub Copilot 实战指南 — 进阶玩法:Coding Agent、自定义指令与 MCP

2026-09-20

GitHub Copilot 实战指南 — 核心功能深度解析:补全、Chat 与 Agent Mode

2026-09-20

GitHub Copilot 实战指南 — 快速上手:注册免费版 + 第一次 AI 编程

2026-09-20

GitHub Copilot 实战指南 — GitHub Copilot 是什么:全球最大的 AI 编程助手

2026-09-20
查看全部文章 →
JR Academy
全球华人学习 AI 第一站
✓15000+ 学员
✓50+ 课程
✓AI 驱动学习平台
训练营免费资源AI学习职业辅导
精选推荐
AI 职业影响地图
测测你的职业风险等级,查看转型路径与学习方向
热门工具
AI & 数据训练营
系统化课程 + 真实项目实战,快速提升竞争力
热门课程
1v1 就业辅导
资深导师一对一指导,简历优化 + 面试准备
就业保障
企业内训定制
AI 技能培训方案,助力团队升级
企业服务
热门标签
Vibe CodingAI 编程CursorClaude求职攻略Prompt前端开发后端开发
订阅更新

获取最新 AI 学习资源、技术教程和求职攻略,直接送达邮箱。

我们尊重您的隐私,不会发送垃圾邮件