Tool confirmation preview has noticeable delay after LLM response completes
Tool confirmation preview has noticeable delay after LLM response completes
Description
There is a noticeable delay between when the LLM finishes streaming its response and when the tool confirmation prompt appears to the user. This delay is especially apparent when using local LLMs where you can observe the server logs showing "Finished streaming response" several seconds before the tool confirmation UI appears.
Environment
- OS: Linux (WSL2)
- Node.js version: 18+
- Nanocoder version: 1.17.2
- AI Provider: LM Studio (local) with OpenAI-compatible API
Steps to Reproduce
- Configure nanocoder to use a local LLM via LM Studio or similar
- Ask the LLM to perform a task that requires tool confirmation (e.g.,
replace_linesorinsert_lines) - Observe the LM Studio server logs - note when "Finished streaming response" appears
- Observe nanocoder TUI - note when the tool confirmation prompt appears
- There is a delay of 1-3+ seconds between these events
Expected Behavior
Tool confirmation prompt should appear immediately (or near-immediately) after the LLM response completes.
Actual Behavior
There is a noticeable delay (1-3+ seconds per tool) between LLM completion and tool confirmation display.
Root Cause Analysis
The delay occurs in source/components/tool-confirmation.tsx lines 50-102. When the tool confirmation component mounts, it runs a useEffect that:
- Runs the validator (reads the file from disk to validate line numbers)
- Runs the formatter (reads the SAME file again to generate a diff preview)
- Applies syntax highlighting via
cli-highlight
Each of these operations is I/O bound and happens sequentially. For multiple tool calls, this compounds significantly.
For each tool call, this happens sequentially. If there are 5 tool calls, that's:
- 5 file reads for validation
- 5 file reads for formatting
- 5 syntax highlighting passes
That's 10 file reads and 5 highlighting passes just to show the confirmation prompts.
Specific bottlenecks:
- Duplicate file reads: The validator and formatter both read the same file independently
- Syntax highlighting:
cli-highlightprocesses each line for the preview - Sequential processing: Each tool call is processed one at a time
- No caching: File contents are not cached between validator and formatter
Proposed Solutions
Quick Win: File Content Caching
Pass the file content from the validator to the formatter to avoid duplicate reads:
// In tool-confirmation.tsx useEffect
const fileContent = await readFile(absPath, 'utf-8'); // Read once
const validationResult = await validator(parsedArgs, fileContent); // Pass to validator
const preview = await formatter(parsedArgs, undefined, fileContent); // Reuse for formatter
Medium-term: Parallel Processing
Process multiple tool calls in parallel when they operate on different files.
Long-term: Lazy Highlighting
- Skip syntax highlighting for large files
- Or apply highlighting asynchronously after initial render
Additional Context
LM Studio server log showing completion:
[2025-12-01 15:44:36][INFO][openai/gpt-oss-20b] Finished streaming response
The tool confirmation UI appeared approximately 2-3 seconds after this log entry.
Related Files
source/components/tool-confirmation.tsx- Main component with delaysource/tools/replace-lines.tsx- Formatter that reads filessource/tools/insert-lines.tsx- Formatter that reads filessource/tools/read-file.tsx- Validator that reads files
Labels
enhancementperformance
Hey @JimStenstrom - thanks for identifying this. Do you want to tackle a fix or shall we triage somewhere else? :)
I saw that there was refactoring going on for AI SDK. Would that refactor hit this code? Figured I'd ask before working on it.
Hey @JimStenstrom - this probably will hit this code actually. That feature branch isn't final though. @DenizOkcu is prototyping this refactor. Might be worth pausing on this issue until we have a consensus here :)
Very happy to discuss!
I started upgrading AI-SDK v6 Beta, because it has a nice way of introducing human-in-the-loop for tool calls. Have a look at my branch and let me know what you think.
https://github.com/Nano-Collective/nanocoder/tree/feature/better-tool-usage
I am also very happy to just bin my approach to unblock you :-)
No need to bin any work. This is a quality of life issue that can be back shelved.
The upgrade to v6 has been merged. I still saw the delay while testing. Let me know if I can be of any help 🙌
@JimStenstrom After testing with latest main the delay is gone for me. here is a screenscpture from a complex tool call. Could you check as well on your side?
https://github.com/user-attachments/assets/4e00a554-8e6d-4462-8b24-3eef000742e9
I'll sync and take a look using the v6 merged code in the next few days to get a resolution.
Hey everyone! It would be great to validate if this is an issue still with the improvements over the course of the last 2 versions :)