Skip to content

Open source file-system and knowledge based agent template. Build AI agents that stay up to date with your knowledge base

prevent AI exhaustion

Hugo Richardac7b058bc21f
diff --git a/apps/chat/server/api/chats/[id].post.ts b/apps/chat/server/api/chats/[id].post.ts
index b572d41..f604720 100644
--- a/apps/chat/server/api/chats/[id].post.ts
+++ b/apps/chat/server/api/chats/[id].post.ts
@@ -1,4 +1,4 @@
-import { convertToModelMessages, createUIMessageStream, createUIMessageStreamResponse, type UIMessage } from 'ai'
+import { convertToModelMessages, createUIMessageStream, createUIMessageStreamResponse, streamText, type UIMessage } from 'ai'
import { z } from 'zod'
import { db, schema } from '@nuxthub/db'
import { kv } from '@nuxthub/kv'
@@ -299,6 +299,24 @@ export default defineEventHandler(async (event) => {
abortSignal: abortController.signal,
})
writer.merge(result.toUIMessageStream())
+
+ // Fallback: if the agent exhausted all steps on tool calls without
+ // producing any text, do one final call with NO tools to force a response.
+ const agentText = await result.text
+ if (!agentText?.trim()) {
+ log.info('chat', `[${requestId}] Agent produced no text, forcing fallback response`)
+ const agentResponse = await result.response
+ const convertedMessages = await convertToModelMessages(messages)
+ const fallback = streamText({
+ model: effectiveModel,
+ messages: [
+ ...convertedMessages,
+ ...agentResponse.messages,
+ ],
+ abortSignal: abortController.signal,
+ })
+ writer.merge(fallback.toUIMessageStream())
+ }
},
onFinish: async ({ messages: responseMessages }) => {
const dbStartTime = Date.now()
diff --git a/apps/chat/server/utils/bot/ai.ts b/apps/chat/server/utils/bot/ai.ts
index a88ce1c..fe5e209 100644
--- a/apps/chat/server/utils/bot/ai.ts
+++ b/apps/chat/server/utils/bot/ai.ts
@@ -1,6 +1,6 @@
import { generateText, Output } from 'ai'
import { log } from 'evlog'
-import { type AgentConfig, agentConfigSchema, getDefaultConfig, ROUTER_MODEL } from '../router/schema'
+import { type AgentConfig, agentConfigSchema, DEFAULT_MODEL, getDefaultConfig, ROUTER_MODEL } from '../router/schema'
import { ROUTER_SYSTEM_PROMPT } from '../prompts/router'
import { buildBotSystemPrompt, buildBotUserMessage } from '../prompts/bot'
import { createAgent } from '../create-agent'
@@ -93,8 +93,23 @@ export async function generateAIResponse(
metadata: context ? { source: context.source } : undefined,
}).catch(() => {})
- if (!result.text) {
- return `I searched the documentation but couldn't generate a helpful response for:
+ // If the agent exhausted all steps on tool calls without producing text,
+ // do one final call with NO tools to force a text response.
+ if (!result.text?.trim()) {
+ log.info('bot', 'Agent produced no text, forcing fallback generation')
+ const fallback = await generateText({
+ model: DEFAULT_MODEL,
+ messages: [
+ { role: 'user', content: buildBotUserMessage(question, context) },
+ ...result.response.messages,
+ ],
+ })
+ if (fallback.text?.trim()) {
+ return fallback.text
+ }
+ }
+
+ return result.text || `I searched the documentation but couldn't generate a helpful response for:
> ${question}
@@ -102,9 +117,6 @@ export async function generateAIResponse(
- Try rephrasing your question with different keywords
- Check the official documentation directly
- Open a discussion for more complex questions`
- }
-
- return result.text
} catch (error) {
const durationMs = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
diff --git a/apps/chat/server/utils/create-agent.ts b/apps/chat/server/utils/create-agent.ts
index b6bed49..be5ff0b 100644
--- a/apps/chat/server/utils/create-agent.ts
+++ b/apps/chat/server/utils/create-agent.ts
@@ -79,8 +79,8 @@ export function createAgent({
},
prepareStep: ({ stepNumber }) => {
if (isAdminMode) return
- // Force text output on the last 2 steps by removing all tools
- if (stepNumber >= maxSteps - 2) {
+ // Force text output on the last step by removing all tools
+ if (stepNumber >= maxSteps - 1) {
return { toolChoice: 'none' as const, activeTools: [] }
}
},
diff --git a/apps/chat/server/utils/prompts/router.ts b/apps/chat/server/utils/prompts/router.ts
index 36b8d43..cca7fe6 100644
--- a/apps/chat/server/utils/prompts/router.ts
+++ b/apps/chat/server/utils/prompts/router.ts
@@ -3,25 +3,23 @@ Analyze the user's question and determine the appropriate configuration for the
## Classification Guidelines
-**trivial** (maxSteps: 4, model: gemini-2.5-flash-lite)
+**trivial** (maxSteps: 4, model: gemini-3-flash)
- Simple greetings: "Hello", "Thanks", "Hi there"
- Acknowledgments without questions
- Examples: "Hi!", "Thank you!", "Got it"
-**simple** (maxSteps: 8, model: gemini-2.5-flash-lite)
+**simple** (maxSteps: 8, model: gemini-3-flash)
- Single concept lookups: "What is X?", "How to use Y?"
- Direct questions with likely one clear answer in one file
- Examples: "What is useAsyncData?", "How to install Nuxt?", "What does definePageMeta do?"
-**moderate** (maxSteps: 15, model: gemini-3-flash)
+**moderate** (maxSteps: 15, model: claude-sonnet-4.5)
- Comparisons or multi-concept questions requiring 2–5 file reads
- Integration questions requiring exploration of multiple sources
- Examples: "Difference between useFetch and useAsyncData?", "How to use Nuxt Content with Nuxt UI?", "How to set up authentication?"
-**complex** (maxSteps: 25, model: gemini-3-flash or claude-opus-4.5)
+**complex** (maxSteps: 25, model: claude-opus-4.6)
- Debugging scenarios describing errors or unexpected behavior
- Architecture questions spanning multiple systems
- Deep analysis requiring cross-referencing many files
-- Examples: "My SSR hydration is failing with this error...", "How to build a multi-tenant app with NuxtHub?", "Design a full auth flow with sessions and roles"
-
-Use claude-opus-4.5 only for the most complex cases requiring deep reasoning (architecture design, multi-system debugging).`
+- Examples: "My SSR hydration is failing with this error...", "How to build a multi-tenant app with NuxtHub?", "Design a full auth flow with sessions and roles"`
diff --git a/apps/chat/server/utils/router/schema.ts b/apps/chat/server/utils/router/schema.ts
index 4c32175..4b9e377 100644
--- a/apps/chat/server/utils/router/schema.ts
+++ b/apps/chat/server/utils/router/schema.ts
@@ -12,9 +12,9 @@ export const agentConfigSchema = z.object({
model: z.enum([
'google/gemini-3-flash',
- 'google/gemini-2.5-flash-lite',
- 'anthropic/claude-opus-4.5',
- ]).describe('flash-lite for trivial/simple, flash for moderate, opus for complex'),
+ 'anthropic/claude-sonnet-4.5',
+ 'anthropic/claude-opus-4.6',
+ ]).describe('flash for trivial/simple, sonnet for moderate, opus for complex'),
reasoning: z.string().max(200)
.describe('Brief explanation of the classification'),
@@ -26,7 +26,7 @@ export function getDefaultConfig(): AgentConfig {
return {
complexity: 'moderate',
maxSteps: 15,
- model: 'google/gemini-3-flash',
+ model: 'anthropic/claude-sonnet-4.5',
reasoning: 'Default fallback configuration',
}
}