agentskit.js
Recipes

Self-debug tool

On tool error, let the agent read the error + schema and draft corrected arguments for a retry.

Tool calls fail for boring reasons: the model hallucinated a field, missed a required arg, or passed a string where a number was expected. wrapToolWithSelfDebug gives your tool a feedback loop β€” on failure, a user-supplied "debugger" sees the error + schema + args and returns corrected arguments for a retry.

#Install

Ships in @agentskit/core/self-debug subpath (no main-bundle weight).

#Wrap any tool

import { wrapToolWithSelfDebug, createLlmSelfDebugger } from '@agentskit/core/self-debug'
import { anthropic } from '@agentskit/adapters'
import { createRuntime } from '@agentskit/runtime'

const smart = anthropic({ apiKey: process.env.ANTHROPIC_API_KEY!, model: 'claude-haiku-4-5' })

async function complete(prompt: string): Promise<string> {
  const runtime = createRuntime({ adapter: smart })
  const r = await runtime.run(prompt)
  return r.content
}

const resilientSearch = wrapToolWithSelfDebug(
  searchTool,
  createLlmSelfDebugger(complete),
  { maxAttempts: 2 },
)

The LLM-backed debugger sees:

  1. The tool's name, description, and JSON Schema.
  2. The previous attempt's arguments.
  3. The error message.

It emits corrected JSON. If it cannot recover, it returns {"giveUp": true} and the original error is rethrown.

#Custom debuggers

You don't have to use an LLM β€” any heuristic works:

const pinnedRetry = wrapToolWithSelfDebug(tool, ({ error, args }) => {
  if (/unknown field "limit"/.test(error.message)) {
    const { limit: _, ...rest } = args
    return { args: rest }
  }
  return { args: null }
})

#Observability

wrapToolWithSelfDebug(tool, debugger, {
  maxAttempts: 3,
  onEvent: e => logger.info('[self-debug]', e),
})

Events: success / failure / retry / give-up.

#See also

Explore nearby

✎ Edit this page on GitHubΒ·Found a problem? Open an issue β†’Β·How to contribute β†’

On this page