コンテンツにスキップ

エラーハンドリング

エラー発生状況回復方法
AgentError一般的なエージェントの失敗コンテキストのために stepId を確認
ToolErrorツールが実行中にスローされるtoolName を確認し、ツールハンドラーを修正
MaxStepsErrorReActループが maxSteps に達したmaxSteps を増やすか、タスクを簡素化
エラー発生状況回復方法
SchiftErrorAPI呼び出しが失敗statuscode を確認
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}`);
}
}

デフォルトの 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("エージェントは最終的な答えに達しませんでした");
}

ツールハンドラーのエラーはキャッチされ、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,
};
}
},
};
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}`);
}
}