OpenClaw - 智能 Agent 框架接入
OpenClaw 是一个流行的开源 AI Agent 框架,内置工具调用、记忆、计划等能力。本文演示如何将 Starsg 设置为 OpenClaw 的统一推理后端。
重要开始前请先准备好在 控制台 创建的 API Key。如果还没有 Key,点击链接前往创建。
1. 安装
pip install openclaw openai
2. 配置环境变量
export OPENCLAW_BASE_URL="https://api.starsg.ai/v1" export OPENCLAW_API_KEY="sk-abc...xyz" export OPENCLAW_MODEL="gpt-4o"
3. 编写 Agent
from openclaw import Agent, tool
@tool
def search_web(query: str) -> str:
"""Search the web"""
return do_search(query)
agent = Agent(
name="researcher",
tools=[search_web],
backend="openai", # Starsg speaks the OpenAI protocol
)
result = agent.run("Research the most exciting open-source LLMs of 2026")
print(result.output)4. 多模型 fallback
OpenClaw 支持按优先级配置多个模型,Starsg 的统一接入让 fallback 极其简单:
agent = Agent(
name="writer",
backend="openai",
fallback_models=[
"gpt-4o",
"claude-opus-4-7",
],
)5. 监控与计费
所有 Agent 调用都会记入 控制台 → 用量分析,你可以按 API Key、模型、时间窗口查看消耗;为每个 Agent 单独申请一把 API Key 即可在控制台直接对齐 Agent 维度的成本与延迟。