examples/test_app.py: LLM request/response logging wrapper re-wraps chat_model.ainvoke/astream on every chat_model access, duplicating LLM REQUEST/RESPONSE log lines #173
Labels
No labels
auto/blocked-by-deps
auto/ci-timeout
auto/claimed-implementer
auto/claimed-merge
auto/claimed-reviewer
auto/driver-down
auto/invariant-violation
auto/last-attempt-tier-0
auto/last-attempt-tier-1
auto/last-attempt-tier-2
auto/last-attempt-tier-min
Automation Tracking
auto/needs-conflict-resolution
auto/needs-implementer
auto/postmortem
auto/ready-to-merge
auto/restart-throttled
auto/revert
auto/sentinel
auto/stale-inactivity
auto/unstable
Blocked
Bounty
$100
Bounty
$1000
Bounty
$10000
Bounty
$20
Bounty
$2000
Bounty
$250
Bounty
$50
Bounty
$500
Bounty
$5000
Bounty
$750
MoSCoW
Could have
MoSCoW
Must have
MoSCoW
Should have
Needs Feedback
Points
1
Points
13
Points
2
Points
21
Points
3
Points
34
Points
5
Points
55
Points
8
Points
88
Priority
Backlog
Priority
CI Blocker
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Signed-off: Owner
Signed-off: Scrum Master
Signed-off: Tech Lead
Spike
State
Completed
State
Duplicate
State
In Progress
State
In Review
State
Paused
State
Unverified
State
Verified
State
Wont Do
Type
Automation
Type
Bug
Type
Discussion
Type
Documentation
Type
Epic
Type
Feature
Type
Legendary
Type
Refactor
Type
Support
Type
Task
Type
Testing
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Depends on
#174 TDD: examples/test_app.py: LLM request/response logging wrapper re-wraps chat_model.ainvoke/astream on every chat_model access, duplicating LLM REQUEST/RESPONSE log lines
cleveragents/cleveractors-core
#186 fix(examples): wrap chat_model ainvoke/astream logging exactly once per instance
cleveragents/cleveractors-core
Reference
cleveragents/cleveractors-core#173
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Metadata
Commit Message:
fix(examples): wrap chat_model ainvoke/astream logging exactly once per instanceBranch: bugfix/m1-llm-request-log-duplication
Background and context
examples/test_app.py's_setup_request_logging()installs an AOP intercept (_patched_ensure_chat_model) that monkey-patchesLLMAgent._ensure_chat_modelto log every LLM request/response tollm_requests.log, purely for observability while running the example CLI. The top-level patch install is documented and guarded as idempotent (_request_logging_configuredprevents the monkey-patch itself from being applied twice), but the body of the patched method has no equivalent guard around the per-chat-model wrap it performs.Current behavior
LLMAgent.chat_modelis a property whose getter callsself._ensure_chat_model()on every access (src/cleveractors/agents/llm.py). This means_ensure_chat_model()runs once for every real LLM call made during a process's lifetime, across every actor/node in a run -- not just once perLLMAgentinstance.Inside
_patched_ensure_chat_model(examples/test_app.py), each invocation does, roughly:There is no check for "is
ainvoke/astreamalready wrapped?" before wrapping again._original_ensure_chat_modelonly skips re-creating the underlyingBaseChatModelonceself._chat_modelis set -- it does not prevent the patched method from re-running its wrap logic. So every real_ensure_chat_model()call across the whole run installs one more logging layer on top of the previous one(s).Result: the Nth real LLM call in a session gets logged N times -- once per stacked wrapper layer -- all synchronously, before/after the single real network round-trip underneath. Observed directly in a production run of the example CLI (
python test_app.py test --llm-protocol http --llm-ip <host> --llm-port 8000 --local-store packages --unsafe --prompt "..." --graph 80487-fpu-actor-tools-graph.yaml): one real completion (providerid: chatcmpl-d129c30def4d4b6b963b04f4,usage_metadata.total_tokens: 88909) was logged as 40 near-identicalLLM REQUEST:/LLM RESPONSE:line pairs inllm_requests.log. Grouping every logged response in that run by its provideridconfirms the pattern is systemic, not a one-off: successive real calls in the same run duplicated 44, 44, 42, 42, 40, 40, 40, 38, 38, 38... times, growing by roughly one extra layer per real call made so far.This does not duplicate the actual outbound API calls or token spend -- confirmed: one real HTTP round-trip per group, identical token counts and completion
idacross every duplicate line in the group. But it inflatesllm_requests.logby a large, growing multiplier, makes the log actively misleading when diagnosing a run (a single real response reads as if the model retried dozens of times), and log volume grows roughly with the square of the number of LLM calls in a long-running session.Expected behavior
chat_model.ainvokeandchat_model.astreamare wrapped for logging exactly once perchat_modelinstance, regardless of how many times_ensure_chat_model()is subsequently called on the owningLLMAgent. Each real LLM call produces exactly oneLLM REQUEST:line and exactly oneLLM RESPONSE:line (or exactly oneLLM STREAM REQUEST:/LLM STREAM RESPONSE:pair for streaming) inllm_requests.log.Acceptance criteria
LLM REQUEST:lines and NLLM RESPONSE:lines inllm_requests.log(1:1 -- no growth multiplier)._ensure_chat_model()more than once on the sameLLMAgent(e.g. once per turn in theinteractiveloop, or once per node execution across a graph run) does not add additional logging layers to an already-wrappedchat_model._ensure_chat_model()twice on the sameLLMAgent/chat_modelinstance and then callingainvokeonce results in exactly one logged request and one logged response.Supporting information
_setup_request_logging._patched_ensure_chat_modelinexamples/test_app.py.LLMAgent.chat_model/LLMAgent._ensure_chat_modelinsrc/cleveractors/agents/llm.py(this part of the core library is correct and not implicated -- it only guards re-creation ofself._chat_model, which is a separate, working concern).llm_requests_prematurely_eneded_the_execution.log) from a CleverActors CLI run that also hit an unrelated, separately-tracked failure (afile_writetool call receiving a corrupted path argument) -- that second issue is out of scope here and is not part of this ticket.examples/), not insrc/cleveractors/runtime code.Subtasks
getattr(chat_model, "_request_logging_wrapped", False)/object.__setattr__(chat_model, "_request_logging_wrapped", True)) in_patched_ensure_chat_modelsoainvokeandastreamare wrapped only once perchat_modelobject.features/that calls the patched_ensure_chat_model()twice on the same agent/chat-model instance and asserts exactly one logged request/response pair per realainvokecall.nox -s coverage_report.nox(all default sessions), fix any errors.Definition of Done
This issue is complete when:
Type/TestingTDD issue exists, the TDD scenario (tagged@tdd_expected_fail) is merged first, and this issue's Forgejo dependency correctly shows it depends on that TDD issue.Note for triage
No existing Epic cleanly covers example/tooling-harness defects (Epic #77 "LLM Agent Runtime Stabilization" explicitly scopes out logging-refactor-only work unless requested; #148 is the single, non-Epic issue that introduced
examples/). Leaving parent-Epic assignment to maintainer triage rather than forcing a mismatched link.Blocked by #174 (TDD issue-capture test must be written and merged first, per the mandatory Bug Fix TDD Workflow).
Fix submitted: PR #186 (bugfix/m1-llm-request-log-duplication -> develop). Moving to State/In Review.