Say that a resident phones the council about an old fridge. The AI agent on the line will reply that it has no page on the subject, although two pages of the guidance are about fridges. Because the agent's one tool fetches a page by its slug, the model has to make up a slug that matches no page.
The author of a 2026 diagnostic benchmark sorts failed tool use into four kinds, which are a skipped call, an ignored result, a fabricated output, and an unnecessary call. Across nineteen models, the best model reaches a clean tool-use rate of 86.33% (arXiv 2607.04686). The shape of a tool sets which of those four failures a model can make.
The model on this page is a scripted mock, so the code prints the same text every time that you execute it. Replace the mock with a model string when you want a real model to choose the calls. Each code block below holds one whole file.
A tool that takes only an identifier
readPage takes a slug and returns the page with that slug, which helps when the conversation already contains a slug. However, the resident's question contains no slug, so the model has to guess one. Because no page has the guessed slug, narsil.get returns undefined, which the tool turns into null for the model.
import { createNarsil } from '@delali/narsil'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { helplineModel, QUESTION, type WastePage } from './mock-model.ts'
const narsil = await createNarsil()
await narsil.createIndex('waste_pages', {
schema: { title: 'string', body: 'string', service: 'enum' },
language: 'english',
})
await narsil.insertBatch('waste_pages', [
{
id: 'bulky-waste',
title: 'Book a bulky waste collection',
service: 'collections',
body: 'A fridge, a freezer, a mattress, or a sofa goes on a bulky waste collection, which costs £22 for up to three items. The crew takes the items from the kerbside on the morning of the booking.',
},
{
id: 'recycling-centre',
title: 'Using the household recycling centre',
service: 'sites',
body: 'The household recycling centre on Marsh Lane takes a fridge, garden waste, and rubble free of charge from any resident holding a site permit.',
},
{
id: 'missed-collection',
title: 'Report a missed collection',
service: 'collections',
body: 'Report a missed bin within two working days and the crew returns to the street on the next working day.',
},
{
id: 'food-waste',
title: 'Food waste caddies',
service: 'collections',
body: 'Every household has a kitchen caddy and a kerbside caddy for food waste, emptied each week.',
},
{
id: 'small-electricals',
title: 'Small electrical items',
service: 'sites',
body: 'A kettle, a toaster, or a hairdryer goes in the small electricals bank at any of the four recycling banks.',
},
{
id: 'garden-waste',
title: 'Garden waste subscription',
service: 'collections',
body: 'A garden waste subscription costs £48 a year and covers a fortnightly collection from March to November.',
},
])
const helplineTools = {
readPage: tool({
description: 'Read one page of the waste and recycling guidance by its slug.',
inputSchema: z.object({ slug: z.string() }),
execute: async ({ slug }: { slug: string }): Promise<WastePage | null> => {
const page = await narsil.get('waste_pages', slug)
if (page === undefined) return null
return { title: String(page.title), body: String(page.body) }
},
}),
}
const askHelpline = async (): Promise<string> => {
const helpline = new ToolLoopAgent({
model: helplineModel(),
instructions: 'Answer the resident from the waste and recycling guidance.',
tools: helplineTools,
})
const reply = await helpline.generate({ prompt: QUESTION })
return reply.text
}
console.log(JSON.stringify({ question: QUESTION, helplineAnswered: await askHelpline() }, null, 2))
await narsil.shutdown(){
"question": "How do I get rid of an old fridge?",
"helplineAnswered": "I hold no page under 'fridge-disposal'."
}The guidance contains the answer twice, on the bulky waste page and on the recycling centre page, yet the resident hears neither. Keep a lookup by key alongside a search all the same, because once a search returns a page's id, the model can use the lookup to open that page.
Search on the words that a resident uses
searchPages takes the question as text and returns the pages that match it. query passes that text through the index's language module, which stems each word and drops the stop words, so the engine matches a page only on the terms that remain. Of those terms, only 'fridge' appears in the guidance, on two pages, so the search returns both of them.
import { createNarsil } from '@delali/narsil'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { helplineModel, QUESTION, type WastePage } from './mock-model.ts'
const narsil = await createNarsil()
await narsil.createIndex('waste_pages', {
schema: { title: 'string', body: 'string', service: 'enum' },
language: 'english',
})
await narsil.insertBatch('waste_pages', [
{
id: 'bulky-waste',
title: 'Book a bulky waste collection',
service: 'collections',
body: 'A fridge, a freezer, a mattress, or a sofa goes on a bulky waste collection, which costs £22 for up to three items. The crew takes the items from the kerbside on the morning of the booking.',
},
{
id: 'recycling-centre',
title: 'Using the household recycling centre',
service: 'sites',
body: 'The household recycling centre on Marsh Lane takes a fridge, garden waste, and rubble free of charge from any resident holding a site permit.',
},
{
id: 'missed-collection',
title: 'Report a missed collection',
service: 'collections',
body: 'Report a missed bin within two working days and the crew returns to the street on the next working day.',
},
{
id: 'food-waste',
title: 'Food waste caddies',
service: 'collections',
body: 'Every household has a kitchen caddy and a kerbside caddy for food waste, emptied each week.',
},
{
id: 'small-electricals',
title: 'Small electrical items',
service: 'sites',
body: 'A kettle, a toaster, or a hairdryer goes in the small electricals bank at any of the four recycling banks.',
},
{
id: 'garden-waste',
title: 'Garden waste subscription',
service: 'collections',
body: 'A garden waste subscription costs £48 a year and covers a fortnightly collection from March to November.',
},
])
const helplineTools = {
readPage: tool({
description: 'Read one page of the waste and recycling guidance by its slug.',
inputSchema: z.object({ slug: z.string() }),
execute: async ({ slug }: { slug: string }): Promise<WastePage | null> => {
const page = await narsil.get('waste_pages', slug)
if (page === undefined) return null
return { title: String(page.title), body: String(page.body) }
searchPages: tool({
description: 'Search the waste and recycling guidance by what its text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<WastePage[]> => {
const found = await narsil.query<WastePage>('waste_pages', { term: text, limit: 5 })
return found.hits.map((hit) => hit.document)
},
}),
}
const askHelpline = async (): Promise<string> => {
const helpline = new ToolLoopAgent({
model: helplineModel(),
instructions: 'Answer the resident from the waste and recycling guidance.',
tools: helplineTools,
})
const reply = await helpline.generate({ prompt: QUESTION })
return reply.text
}
console.log(JSON.stringify({ question: QUESTION, helplineAnswered: await askHelpline() }, null, 2))
await narsil.shutdown(){
"question": "How do I get rid of an old fridge?",
"helplineAnswered": "The household recycling centre on Marsh Lane takes a fridge, garden waste, and rubble free of charge from any resident holding a site permit. A fridge, a freezer, a mattress, or a sofa goes on a bulky waste collection, which costs £22 for up to three items. The crew takes the items from the kerbside on the morning of the booking."
}With this tool, the resident hears both ways to get rid of the fridge. However, the tool has two faults that you can fix. It returns each hit as the whole stored document, which means that the next prompt contains every field of every page. It also drops each hit's score, so the model has no way to tell the weaker page from the stronger one.
Narrow by service, and fix the rest in the tool
You control what the model receives with four query options:
document.includelists the two stored fields,titleandbody, that the engine returns with each hit.- The tool sets
limitto 3 in its code, so only your application chooses how many pages the model receives. - With
filters, the engine removes candidates before it scores anything. - With
facets, the engine returns a count of the matching pages in each service area along with the hits.
SERVICE_AREAS holds the council's names for its service areas. Because z.enum puts that list in the tool's input schema, the model can choose only from those values. Once the tool returns one match in each area in the facet counts, the model narrows the search to collections on its second call. Because the engine counts every value exactly on a single thread, it returns the service facet with errorBound: 0, which means that each count is the exact number of matching pages.
import { createNarsil } from '@delali/narsil'
import { ToolLoopAgent, tool } from 'ai'
import { z } from 'zod'
import { helplineModel, QUESTION, type WastePage } from './mock-model.ts'
import { helplineModel, type PageSearch, QUESTION, type WastePage } from './mock-model.ts'
const SERVICE_AREAS = ['collections', 'sites'] as const
const narsil = await createNarsil()
await narsil.createIndex('waste_pages', {
schema: { title: 'string', body: 'string', service: 'enum' },
language: 'english',
})
await narsil.insertBatch('waste_pages', [
{
id: 'bulky-waste',
title: 'Book a bulky waste collection',
service: 'collections',
body: 'A fridge, a freezer, a mattress, or a sofa goes on a bulky waste collection, which costs £22 for up to three items. The crew takes the items from the kerbside on the morning of the booking.',
},
{
id: 'recycling-centre',
title: 'Using the household recycling centre',
service: 'sites',
body: 'The household recycling centre on Marsh Lane takes a fridge, garden waste, and rubble free of charge from any resident holding a site permit.',
},
{
id: 'missed-collection',
title: 'Report a missed collection',
service: 'collections',
body: 'Report a missed bin within two working days and the crew returns to the street on the next working day.',
},
{
id: 'food-waste',
title: 'Food waste caddies',
service: 'collections',
body: 'Every household has a kitchen caddy and a kerbside caddy for food waste, emptied each week.',
},
{
id: 'small-electricals',
title: 'Small electrical items',
service: 'sites',
body: 'A kettle, a toaster, or a hairdryer goes in the small electricals bank at any of the four recycling banks.',
},
{
id: 'garden-waste',
title: 'Garden waste subscription',
service: 'collections',
body: 'A garden waste subscription costs £48 a year and covers a fortnightly collection from March to November.',
},
])
const helplineTools = {
searchPages: tool({
description: 'Search the waste and recycling guidance by what its text says.',
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }: { text: string }): Promise<WastePage[]> => {
const found = await narsil.query<WastePage>('waste_pages', { term: text, limit: 5 })
return found.hits.map((hit) => hit.document)
inputSchema: z.object({ text: z.string(), service: z.enum(SERVICE_AREAS).optional() }),
execute: async ({
text,
service,
}: {
text: string
service?: (typeof SERVICE_AREAS)[number]
}): Promise<PageSearch> => {
const found = await narsil.query<WastePage>('waste_pages', {
term: text,
filters: service === undefined ? undefined : { fields: { service: { eq: service } } },
facets: { service: {} },
document: { include: ['title', 'body'] },
limit: 3,
})
return {
hits: found.hits.map((hit) => ({
page: hit.id,
title: hit.document.title,
body: hit.document.body,
score: Number((hit.score ?? 0).toFixed(2)),
})),
services: found.facets?.service.values ?? {},
}
},
}),
}
const askHelpline = async (): Promise<string> => {
const helpline = new ToolLoopAgent({
model: helplineModel(),
instructions: 'Answer the resident from the waste and recycling guidance.',
instructions: 'Answer the resident from the waste and recycling guidance, and say which page you used.',
tools: helplineTools,
})
const reply = await helpline.generate({ prompt: QUESTION })
return reply.text
}
console.log(JSON.stringify({ question: QUESTION, helplineAnswered: await askHelpline() }, null, 2))
await narsil.shutdown(){
"question": "How do I get rid of an old fridge?",
"helplineAnswered": "A fridge appears on 1 collections page and 1 sites page. See 'Book a bulky waste collection'. A fridge, a freezer, a mattress, or a sofa goes on a bulky waste collection, which costs £22 for up to three items. The crew takes the items from the kerbside on the morning of the booking."
}On this step, the model cuts the question down to fridge, which is the one word of the question that the guidance contains. On its second call, it narrows the search by service to the bulky waste page, so its answer contains that page's title along with the £22 charge.
- Model calls
readPage({ slug: 'fridge-disposal' }) - Tool returns
null - Model calls
searchPages({ text: 'fridge' }) - Tool returns
2 hits, services: { collections: 1, sites: 1 } - Model calls
searchPages({ text: 'fridge', service: 'collections' }) - Tool returns
1 hit: bulky-waste
Where to go next
- Read Filters and facets for the filter operators, the facet counts, and their
errorBound. - Read Fit the passages in a context budget for how to measure, in characters, what a tool sends back to the model.
- Read Cite the passage behind an answer for how to turn a hit into a citation with the reference and the offsets.