Skip to content

refactor(dspy): use acall usage tracking#99

Merged
enitrat merged 3 commits into
mainfrom
refactor/dspy-usage-acall
Jan 3, 2026
Merged

refactor(dspy): use acall usage tracking#99
enitrat merged 3 commits into
mainfrom
refactor/dspy-usage-acall

Conversation

@enitrat
Copy link
Copy Markdown
Collaborator

@enitrat enitrat commented Jan 3, 2026

Summary

  • Switch DSPy module calls to and rely on prediction usage tracking
  • Replace manual aggregation with tracker for streaming usage
  • Update tests/mocks for and optional optimizer artifacts

Context

DSPy attaches usage in module wrappers; moving to avoids missing usage and centralizes tracking.

Test Plan

  • PYTHONPATH=python .venv/bin/python -m pytest -q python/tests

Copy link
Copy Markdown
Collaborator Author

@enitrat enitrat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AUTOMATED]

Summary

This PR refactors DSPy module invocations from aforward to acall to leverage DSPy's built-in usage tracking mechanism, and replaces manual token usage accumulation with dspy.track_usage() context manager for streaming. This is a solid architectural improvement that simplifies code and improves reliability.

Strengths

  • Cleaner usage tracking: Removing manual _accumulate_usage and relying on DSPy's acall / track_usage() is the right approach
  • Reduced complexity: ~11 fewer lines of production code with less manual plumbing
  • Better separation of concerns: Usage tracking is now handled by the framework where it belongs
  • Comprehensive test updates: All mocks properly updated to include acall alongside existing methods

Minor Observations

  • The combine_usage function in types.py is no longer imported in rag_pipeline.py but remains in the codebase (used elsewhere or could be dead code)
  • The optimizer_artifacts_optional fixture globally sets OPTIMIZER_RUN=1 which is a reasonable approach for test isolation

Overall, this is a well-executed refactor that improves code quality. Tests pass according to the PR description.

Comment thread python/src/cairo_coder/server/app.py
Comment thread python/src/cairo_coder/core/rag_pipeline.py Outdated
Comment thread python/tests/conftest.py
Comment thread python/src/cairo_coder/dspy/generation_program.py
enitrat and others added 2 commits January 3, 2026 20:39
- Remove unused `combine_usage` function from types.py
- Remove corresponding `TestCombineUsage` tests
- Add null guard with warning log for `lm_usage` in server/app.py

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@enitrat enitrat force-pushed the refactor/dspy-usage-acall branch from 2a6fd82 to 040e0b9 Compare January 3, 2026 21:10
@enitrat enitrat merged commit 4b795ee into main Jan 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant