kimi-cli icon indicating copy to clipboard operation
kimi-cli copied to clipboard

Kimi CLI stuck in reading one file again and again and stuck in a loop

Open isbafatima90-arch opened this issue 10 months ago • 16 comments

What version of Kimi CLI is running?

0.76

Which open platform/subscription were you using?

custom anthropic endpoint model using config.toml

Which model were you using?

mimo-v2-flash

What platform is your computer?

Linux 6.18.3-arch1-1 x86_64 omarchy

What issue are you seeing?

Image

config.toml.txt

What steps can reproduce the bug?

This bug ONLY happens when the Kimi CLI auto compacts after long sessions and after it auto compacts it can get stuck in the loop of reading the same file over and over again.

What is the expected behavior?

It should read a single file once and if its a very long file and needs to continue reading it then max maby 2-3 times.

Additional information

Have a good day kimi cli devs! this is awsome cli tool.

isbafatima90-arch avatar Jan 19 '26 05:01 isbafatima90-arch

Could you try reproducing this with other models? This issue is likely caused by the Flash model's poor compression quality. Testing with stronger models (like kimi-k2-turbo-preview or kimi-k2-thinking) would help confirm if this is model-specific.

xiaoju111a avatar Jan 19 '26 05:01 xiaoju111a

It's possible for some third-party models to trap themselves into an infinite loop. We do have plan to add some simple restriction in Kimi CLI to try to avoid this, but what's probably more important is to report to the model provider.

stdrc avatar Jan 19 '26 05:01 stdrc

It's possible for some third-party models to trap themselves into an infinite loop. We do have plan to add some simple restriction in Kimi CLI to try to avoid this, but what's probably more important is to report to the model provider.

please add restriction feature to avoid this loop.

isbafatima90-arch avatar Jan 19 '26 05:01 isbafatima90-arch

I'm experiencing a similar looping behavior, though my setup is slightly different.

My Configuration:

  • LLM: Kimi K2.5 (third-party deployment, not official moonshot API)
  • AI Coding Tool: opencode and Claude

After investigating, I believe the repetitive output/looping behavior is rooted in the base LLM itself rather than the CLI tool implementation. This would explain why it occurs across different frontends (kimi-cli, opencode, Claude etc.) when using the same underlying model.

Additional observation: This issue doesn't appear to be related to context window size. I've encountered this repetitive output behavior even with relatively small context windows (less than 30k tokens), suggesting it's not simply a matter of context length or compression.

PrideChung avatar Mar 06 '26 09:03 PrideChung

I have similar problem, when using Kimi K2.6 with Claude - so not sure it would be helpful for the kimi-cli, but sharing anyways as it looks like the model has tool calling issues.

Here's postmortem written:


Postmortem: Background Review Agents Stuck in Infinite Read Retry Loop

Date: 2026-04-21 Status: Root cause identified, fix pending Affected workflow: /review-impl skill (5 parallel review agents)

Summary

During a post-implementation code review of the assignmentPushCache feature, 3 of 5 parallel background review agents launched via the Task tool entered an infinite retry loop. Each agent repeatedly called Read on srv/shinto/api/server/operator_install_assignments.go with identical parameters (limit: 30, offset: 49). The tool returned a "File unchanged since last read" reference message instead of content. The agent did not know how to resolve this reference and retried the same call indefinitely, consuming tokens and wall-clock time until manually killed.

The 2 agents that completed successfully (Spec Compliance, Architecture) read files the parent conversation had not recently accessed, so they received actual content on the first try.

Root Cause

Background agents do not inherit parent conversation file cache

When a background agent calls Read on a file that was already read in the parent conversation, the tool returns:

"File unchanged since last read. The content from the earlier Read tool_result in this conversation is still current — refer to that instead of re-reading."

This message is a cache hit reference — it tells the parent session to reuse prior content. But background agents run in isolated contexts and cannot see the parent conversation's prior tool results. When they receive this message, they lack the referenced content and cannot proceed.

The agent prompt instructed file exploration without content injection

The review agent prompts included instructions like:

"DO NOT trust implementer claims. Read the actual code." "Focus on files: operator_install_assignments.go, server.go"

The agents attempted to read these files to fulfill the prompt. Because the parent conversation had recently read the same files, the tool returned cache-hit references. The agent's retry logic kept attempting the same Read call with identical parameters, never breaking out of the loop.

Evidence

Agent a8b1ed4a0831ea25d (Completeness review) log excerpt:

06:19:48 Read operator_install_assignments.go limit=30 offset=49
06:19:58 "File unchanged since last read..."
06:20:04 Read operator_install_assignments.go limit=30 offset=49
06:20:12 "File unchanged since last read..."
06:20:20 Read operator_install_assignments.go limit=30 offset=49
... (repeated 30+ times over ~6 minutes)

Total cache read tokens consumed: ~120,000 (wasted). Agent never produced a review.

Contributing Factors

  1. Large file scope for background agents. The prompts asked agents to explore and review code without providing exact line ranges or inline content. Agents defaulted to reading the same file repeatedly.

  2. No timeout or iteration limit on agent tool loops. The agent had no mechanism to detect it was stuck and exit gracefully.

  3. The Read tool's cache-hit message is not actionable for isolated agents. The message assumes access to parent conversation context, which background agents don't have.

Impact

  • 3 of 5 review agents failed to produce output (Completeness, Code Quality, Product Context)
  • ~6 minutes of wall-clock time wasted per stuck agent
  • ~360,000 cache read tokens consumed across 3 agents with zero productive output
  • Manual intervention required — user noticed hang and asked "WHY THIS HANGS AGAIN?"

Fix Plan

Immediate workaround (for this session)

Provide file content inline in agent prompts instead of asking agents to Read files. Example:

Here is the relevant code from operator_install_assignments.go lines 47-70:
[inline code block]

Please review this specific implementation.

Short-term fix (agent prompt templates)

Update the review-impl skill prompt templates to include:

  1. Inline code snippets for the files under review, passed as context in the prompt itself
  2. Exact line ranges — instead of "read operator_install_assignments.go", specify "review lines 47-70 and 157-280"
  3. Fallback instruction: If Read returns "File unchanged since last read", the agent should stop retrying and report that it cannot access the file

Medium-term fix (platform/tooling)

  1. Fix Read tool for background agents — when a background agent requests a file that exists in parent cache, return the actual content instead of a reference message. Background agents have no way to resolve references.

  2. Add agent iteration guardrails — detect when an agent makes the same tool call (>3 times with identical parameters) and auto-terminate with an error message.

  3. Consider foreground agents for code review — foreground agents share conversation context and can resolve cache references. The trade-off is sequential execution instead of parallel.

Lessons Learned

  1. Background agents are not parent-context clones. They run in isolated sessions. Any file content they need must be provided inline or they must use tools that return actual data every time (e.g., Bash with cat).

  2. "Read the actual code" instructions backfire when the tool caches. A prompt that tells an agent to read code is fragile if the Read tool uses conversation-scoped caching.

  3. Watch for repetitive tool calls as a hang signal. If an agent's output file shows the same tool call repeating, it's stuck.

  4. Two successful agents are a control group. The Spec Compliance and Architecture agents succeeded because they accessed files the parent hadn't read. This confirms the root cause is cache-reference unresolvability, not a general agent failure.

What We Lacked

  • No observability into agent progress until completion/kill
  • No automatic detection of repetitive tool-call loops
  • No fallback in agent prompts for "file unchanged" responses

Related

  • 2026-04-15 Operator Upgrade Procedure Debugging — the original incident that motivated the code review
  • docs/plans/2026-04-21-risk-regression-analysis.md — analysis of 22 implementation plans, some reviewed with the same agent harness

xojigsx avatar Apr 21 '26 06:04 xojigsx

Kimi K2.6 + CC 碰到的问题:当 Read 工具返回 “Wasted call — file unchanged since your last Read. Refer to that earlier tool_result instead.” 模型直接陷入死循环。期间没有 Compact。


Image

Lionad-Morotar avatar May 09 '26 16:05 Lionad-Morotar

I have similar problem, when using Kimi K2.6 with Claude Code, please fix it

ziscloud avatar Jun 10 '26 01:06 ziscloud

I have similar problem, when using Kimi K2.6 with Claude Code, please fix it

samehdoush avatar Jun 12 '26 12:06 samehdoush

same on K2.7 + CC,如果需要的话我可以提供对话文件


same on K2.7 + CC, I can provide conversation files if needed

Lionad-Morotar avatar Jun 13 '26 03:06 Lionad-Morotar

I would like to be completely honest about my experience. The loop issue has become so severe that the model is currently almost unusable for my workflow. At this point, it happens on nearly every request. The model frequently gets stuck in repetitive file-reading loops and makes no meaningful progress. The problem becomes even worse when using multiple subagents. Instead of collaborating on the task, they often all end up stuck in the same behavior, repeatedly reading files without moving forward. This results in an enormous amount of token consumption while producing little or no useful output. As a result, the model is currently not reliable for analysis, planning, or large software engineering tasks, which are the primary reasons I subscribed in the first place. I would appreciate a direct and honest answer: is there currently a known solution to this problem, or is there not? If there is a fix, workaround, or planned improvement, please let me know. If there is no solution at this time, I would also appreciate knowing that honestly so I can make an informed decision about whether to renew my subscription in the future. Thank you.

Image

samehdoush avatar Jun 13 '26 11:06 samehdoush

@samehdoush I think this is a harness issue, not a model issue.

I would try two things:

  1. use the original kimi cli if the problem is there (they built harness for their model - so there should not be)
  2. if you still have issues - it would be useful to record all req/resp from iterations - dump into a directory and ask agent to identify reason for looping - usually it will point out problem with specific prompt - and then you have 2 options:

a) if the problem is confusing skill - you can patch it (it would explain why some users have this issue and some not) b) if the problem is system prompt - maybe it would be possible to remedy it via AGENTS.md (e.g. add extra instructions how to use read tool)

xojigsx avatar Jun 13 '26 12:06 xojigsx

@samehdoush I think this is a harness issue, not a model issue.

I would try two things:

  1. use the original kimi cli if the problem is there (they built harness for their model - so there should not be)
  2. if you still have issues - it would be useful to record all req/resp from iterations - dump into a directory and ask agent to identify reason for looping - usually it will point out problem with specific prompt - and then you have 2 options:

a) if the problem is confusing skill - you can patch it (it would explain why some users have this issue and some not) b) if the problem is system prompt - maybe it would be possible to remedy it via AGENTS.md (e.g. add extra instructions how to use read tool)

Thanks for the suggestion.

Interestingly, I do not see this behavior when using the original Kimi CLI. The issue mainly appears when using Kimi through Claude Code and OpenCode, which does seem to support your theory that this may be a harness/tool integration issue rather than a pure model issue.

What makes it particularly noticeable is that once the agent starts looping, it often keeps re-reading the same files repeatedly without making progress. In multi-agent workflows, I've even seen multiple subagents fall into similar file-reading loops at the same time.

Your point about prompts is interesting. Do you have any recommendations for instructions that could be added to CLAUDE.md to reduce the likelihood of these loops? For example, guidance around avoiding repeated reads of the same files, preferring search before reading, or explicitly tracking previously inspected files.

I'd be interested to hear if you've found any prompt patterns that help mitigate this behavior.

samehdoush avatar Jun 13 '26 14:06 samehdoush

@samehdoush Unfortunately I do not have right now a repro, if I happen to encounter it again, will let you know

xojigsx avatar Jun 13 '26 15:06 xojigsx

This bug is really annoying. It blocks my workflow almost every time.

ziscloud avatar Jun 24 '26 15:06 ziscloud

I can confirm this issue happens also with claude cli as harness and kimi2.7-code as the model:

Image Image

mwlik avatar Jun 28 '26 21:06 mwlik

有人愿意尝试一下我的这个工具吗,https://github.com/Lionad-Morotar/cc-break-dead-loop

我认为他有些效果,最近没有再死循环过


Is anyone willing to try this tool of mine, https://github.com/Lionad-Morotar/cc-break-dead-loop

I think it has some effect, and I haven’t been in an endless loop recently

Lionad-Morotar avatar Jul 02 '26 04:07 Lionad-Morotar