第13章 · 免费预计阅读 2 分钟
进阶技巧与最佳实践
从实战经验中总结的技巧,帮你更好地使用 OpenClaw。
Prompt Engineering 技巧
写出高质量的 System Prompt:
# ❌ 不好的 Prompt
system_prompt: "你是一个助手"
# ✅ 好的 Prompt
system_prompt: |
你是一个专业的 Python 后端工程师,专注于:
**技术栈**:
- Python 3.11+, FastAPI, SQLAlchemy
- PostgreSQL, Redis, Celery
- Docker, Kubernetes
**代码规范**:
- 遵循 PEP 8 和 Google Python Style Guide
- 使用类型注解(Type Hints)
- 编写单元测试,覆盖率 > 80%
- 添加详细的 docstring
**响应格式**:
- 先分析问题,再给出方案
- 代码必须可直接运行
- 说明设计决策的理由
关键原则:
- 具体明确的角色定位
- 列出技术栈和工具偏好
- 明确代码风格和规范
- 定义输出格式要求
性能优化
提升 Agent 响应速度:
# 1. 使用流式响应
agent:
response_mode: "streaming"
# 2. 设置合理的超时
timeout:
llm_request: 30s
tool_execution: 60s
# 3. 启用缓存
cache:
enabled: true
ttl: 3600 # 1 小时
semantic_cache:
enabled: true
similarity_threshold: 0.95
# 4. 并行执行工具调用
tools:
parallel_execution: true
max_concurrent: 5
# 5. 模型选择优化
routing:
- pattern: "简单问答"
model: "gpt-3.5-turbo" # 快速便宜
- pattern: "复杂任务"
model: "gpt-4-turbo" # 能力强
安全最佳实践
保护你的 OpenClaw 部署:
security:
# 1. 启用身份验证
auth:
enabled: true
type: "jwt"
secret: "${JWT_SECRET}"
# 2. 限制文件系统访问
file_system:
chroot: "/workspace"
readonly_paths:
- "/etc"
- "/usr"
# 3. 沙箱隔离
sandbox:
enabled: true
network_isolation: true
resource_limits:
cpu: "1.0"
memory: "2GB"
# 4. 敏感信息过滤
content_filter:
enabled: true
patterns:
- "password"
- "api_key"
- "secret"
- "token"
# 5. 审计日志
audit:
enabled: true
log_level: "info"
retention_days: 90
生产部署检查清单
部署到生产环境前的检查项:
- 环境变量: 所有敏感信息使用环境变量
- HTTPS: 配置 SSL/TLS 证书
- 备份: 配置数据库自动备份
- 监控: 接入 Prometheus + Grafana
- 告警: 配置错误告警和性能告警
- 限流: 设置 API 限流和预算控制
- 日志: 集中化日志收集(ELK/Loki)
- 高可用: 至少 2 个实例 + 负载均衡
- 灾难恢复: 准备应急预案和恢复流程
- 文档: 完善运维文档
# 生产环境推荐配置
production:
replicas: 3
resources:
requests:
cpu: "2"
memory: "4Gi"
limits:
cpu: "4"
memory: "8Gi"
autoscaling:
enabled: true
min_replicas: 3
max_replicas: 10
target_cpu: 70
health_check:
liveness_probe:
path: "/health"
interval: 10s
readiness_probe:
path: "/ready"
interval: 5s
掌握这些技巧,你就能构建生产级的 AI Agent 系统。