エラーハンドリング
エラーの種類
Section titled “エラーの種類”エージェントエラー
Section titled “エージェントエラー”| エラー | 発生状況 | 回復方法 |
|---|---|---|
AgentError | 一般的なエージェントの失敗 | コンテキストのために stepId を確認 |
ToolError | ツールが実行中にスローされる | toolName を確認し、ツールハンドラーを修正 |
MaxStepsError | ReActループが maxSteps に達した | maxSteps を増やすか、タスクを簡素化 |
クライアントエラー
Section titled “クライアントエラー”| エラー | 発生状況 | 回復方法 |
|---|---|---|
SchiftError | API呼び出しが失敗 | status と code を確認 |
AuthError | 無効なAPIキー (401) | sch_... キーを確認 |
QuotaError | 使用制限を超えた (402) | プランをアップグレードするか、リセットを待つ |
エージェントエラーのキャッチ
Section titled “エージェントエラーのキャッチ”import { AgentError, MaxStepsError } from "@schift-io/sdk";
try { const result = await agent.run("複雑な質問");} catch (err) { if (err instanceof MaxStepsError) { console.log("エージェントが多くのステップを踏みました。もっと簡単な質問を試してください。"); } else if (err instanceof AgentError) { console.log(`エージェントエラーがステップ ${err.stepId} で発生しました: ${err.message}`); }}最大ステップ
Section titled “最大ステップ”デフォルトの maxSteps は10です。エージェントがより多くのツール呼び出しを必要とする場合は、増やしてください:
const agent = new Agent({ maxSteps: 25, // 最大25回のReActイテレーションを許可 ...});maxSteps に達すると、agent.run() はエラーメッセージを output として返します(スローしません)。最後のステップのタイプを確認するには result.steps をチェックしてください:
const result = await agent.run("...");const lastStep = result.steps[result.steps.length - 1];
if (lastStep.type === "error") { console.log("エージェントは最終的な答えに達しませんでした");}ツールエラーハンドリング
Section titled “ツールエラーハンドリング”ツールハンドラーのエラーはキャッチされ、LLMに返されます。エージェントはエラーを確認し、再試行するか、別のアプローチを使用するかを決定できます。
const riskyTool: AgentTool = { name: "risky_api", description: "信頼性の低いAPIを呼び出す", handler: async (args) => { try { const data = await callUnreliableAPI(args); return { success: true, data }; } catch (err) { return { success: false, data: null, error: err.message, }; } },};APIクライアントエラー
Section titled “APIクライアントエラー”import { SchiftError, AuthError, QuotaError } from "@schift-io/sdk";
try { const results = await rag.search("クエリ");} catch (err) { if (err instanceof AuthError) { console.log("APIキーを確認してください"); } else if (err instanceof QuotaError) { console.log("クォータを超えました -- プランをアップグレードしてください"); } else if (err instanceof SchiftError) { console.log(`APIエラー: ${err.status} ${err.code}`); }}