快速开始
快速开始
前提条件
| 项目 | 值 |
|---|---|
| Node.js | 18+ |
| API Key | JOY_TOKEN_API_KEY |
| 模型 | auto |
第 1 步:安装
$ npm install @joytoken/agent-sdk-ts
第 2 步:设置 API Key
$ export JOY_TOKEN_API_KEY="jt_xxx"
第 3 步:创建 agent
1 import { Agent, createJoyTokenProvider, stepCountIs } from "@joytoken/agent-sdk-ts"; 2 3 const agent = new Agent({ 4 model: createJoyTokenProvider(), 5 modelName: "auto", 6 system: "You are a concise assistant.", 7 stopWhen: [stepCountIs(4)], 8 }); 9 10 const result = await agent.run("Reply with exactly: pong"); 11 12 console.log(result.finalText); 13 console.log(result.usage.totalTokens);
第 4 步:检查结果
| 位置 | 应看到 |
|---|---|
| 程序输出 | pong |
| JoyToken Logs | 请求路径是 /openai/v1/chat/completions |
| JoyToken Usage | tokens 和 credits 归因到这把 Key |
常见错误
| 错误 | 处理方式 |
|---|---|
401 Unauthorized | 检查 JOY_TOKEN_API_KEY |
| agent 没有停止 | 添加 stepCountIs 或 maxToolCalls |
finalText 为空 | 检查模型是否返回 tool call,或查看 result.messages |