Skip to content

Fix prompt caching and improve input/output metrics visualization - #143

Merged
xingyaoww merged 20 commits into
mainfrom
xw/metrics
Sep 7, 2025
Merged

xingyaoww merged 20 commits into
mainfrom
xw/metrics

Conversation

@xingyaoww

Copy link
Copy Markdown
Contributor

Fix prompt caching #137

Improve input/output metrics visualization
image

xingyaoww and others added 3 commits September 6, 2025 16:37
- Display metrics as Panel subtitle instead of panel content
- Format as compact string: 'Accumulated tokens: XX (input), XX (input cached, rate XX%), XX output, XX$'
- Add comma formatting for large numbers (e.g., 1,500 instead of 1500)
- Calculate and display cache hit rate percentage
- Maintain existing fallback behavior when no metrics available

Co-authored-by: openhands <openhands@all-hands.dev>
@xingyaoww
xingyaoww enabled auto-merge (squash) September 6, 2025 21:03
@xingyaoww
xingyaoww disabled auto-merge September 6, 2025 21:04
@xingyaoww

Copy link
Copy Markdown
Contributor Author

@OpenHands please fix the failing actions on PR #143 at branch xw/metrics

We just replace ModelResponse with our custom ModelResposeWithMetrics type. you should adjust the tests to follow this change

@openhands-ai

openhands-ai Bot commented Sep 6, 2025

Copy link
Copy Markdown

I'm on it! xingyaoww can track my progress at all-hands.dev

🟡 Solvability: MEDIUM

This issue involves updating test cases to accommodate a type change from ModelResponse to ModelResponseWithMetrics, which is a well-defined technical modification with clear scope. The request provides specific context about the failing tests and the exact nature of the required changes, making it a moderately solvable issue.

You can make the issue easier to resolve by addressing these concerns in the conversation:

• Include specific error messages from the failing tests to help identify exactly which assertions need updating
• Provide examples of the current test code that's failing to clarify the scope of changes needed
• Share details about the ModelResponseWithMetrics type structure to understand what test modifications are required

Positive feedback:

• The issue clearly identifies the root cause (type replacement) and specific location (PR #143, branch xw/metrics)
• The request uses precise technical terminology that makes the problem scope well-defined
• The issue follows standard software development patterns for test updates after API changes

- Added ModelResponseWithMetrics and Metrics to SDK exports
- Fixed all test files to handle ModelResponseWithMetrics instead of ModelResponse
- Updated test assertions to handle populated metrics data
- Fixed telemetry.py duplicate metrics parameter issue
- Added helper functions to compare responses excluding metrics
- Fixed tool calls test structure
- All 279 tests now passing

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Sep 6, 2025 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report
FileStmtsMissCoverMissing
openhands
   __init__.py10100% 
openhands/sdk
   __init__.py14285%22–23
   logger.py732171%33, 57, 64–67, 69–71, 124, 130–132, 135–136, 142–144, 151, 156–157
openhands/sdk/agent
   __init__.py30100% 
   base.py46491%36, 47, 83, 103
openhands/sdk/agent/agent
   __init__.py20100% 
   agent.py1282878%73–75, 132–133, 135–137, 139–141, 175, 189, 210, 240, 272–277, 284–285, 289–291, 319, 326
openhands/sdk/context
   __init__.py40100% 
   agent_context.py57296%146, 152
   manager.py330%1, 4–5
   view.py47687%46–50, 52
openhands/sdk/context/condenser
   __init__.py30100% 
   condenser.py17382%68–69, 73
   no_op_condenser.py60100% 
openhands/sdk/context/microagents
   __init__.py40100% 
   exceptions.py50100% 
   microagent.py1412979%129, 132–135, 209–215, 218, 223, 245–246, 251–252, 254, 258, 265–267, 275–277, 331, 333–334
   types.py300100% 
openhands/sdk/context/utils
   __init__.py20100% 
   prompt.py30583%12, 15, 24, 44–45
openhands/sdk/conversation
   __init__.py50100% 
   conversation.py83890%70, 78, 86–88, 92–93, 149
   state.py28292%47–48
   types.py30100% 
   visualizer.py1442483%46, 71, 84, 110, 112, 114, 117, 166–168, 171–172, 174, 197, 227, 231, 238, 241, 244–245, 268–270, 272
openhands/sdk/event
   __init__.py60100% 
   base.py642068%34, 38, 49, 53–55, 57–62, 64–65, 67–69, 71–72, 74
   condenser.py25580%29, 33–35, 49
   llm_convertible.py1051486%29–30, 35–36, 174–175, 180, 188, 220–221, 226, 248–249, 254
   types.py30100% 
   user_action.py6183%12
   utils.py110100% 
openhands/sdk/llm
   __init__.py60100% 
   exceptions.py360100% 
   llm.py41413866%269, 274, 287–289, 293–294, 326, 365, 369, 379, 385–386, 403–404, 474, 487–488, 493–494, 496–497, 499–502, 507–509, 513–515, 532–533, 536–538, 541–542, 550–551, 556, 559, 561–562, 566–567, 570–574, 584–587, 592–597, 601–602, 611–613, 616–617, 641, 647–648, 694, 743, 749, 752, 761–762, 771, 778, 781, 785–787, 791, 793–798, 800–817, 820–824, 826–827, 833–842, 846–857
   llm_registry.py380100% 
   message.py100298%201–202
   metadata.py150100% 
openhands/sdk/llm/utils
   fn_call_converter.py34311267%74, 343, 345, 348–349, 367, 369, 375, 381, 383, 422, 424, 426, 428, 433–434, 481–482, 518–520, 522, 524, 545–547, 553, 575, 601–602, 610–613, 615, 617, 639, 648, 656, 701–704, 708–711, 723, 727, 738, 748, 794, 796–798, 800, 803, 819–821, 823–826, 829, 833, 844–845, 859, 866–867, 870–871, 875–876, 882–883, 886, 905–908, 912–913, 918–919, 924, 973–974, 980, 994, 1006, 1008–1009, 1012–1014, 1016–1017, 1023–1025, 1027–1028, 1030, 1032, 1036, 1038, 1043, 1045–1046, 1049
   metrics.py110298%17, 113
   model_features.py400100% 
   telemetry.py113892%71, 94, 99–100, 112–113, 120, 202
openhands/sdk/mcp
   __init__.py50100% 
   client.py631182%41, 56–57, 78, 82, 94–95, 105–106, 112–113
   definition.py27196%54
   tool.py381365%33–36, 40, 43, 46–49, 89–90, 95
   utils.py30486%23–24, 27, 30
openhands/sdk/tool
   __init__.py40100% 
   schema.py89693%19–21, 23, 32, 191
   security_prompt.py30100% 
   tool.py821186%51, 107, 129, 149, 152–158
openhands/sdk/tool/builtins
   __init__.py30100% 
   finish.py150100% 
openhands/sdk/utils
   __init__.py20100% 
   json.py28280%1–3, 5, 7–8, 11, 14–21, 25, 28, 30–31, 34, 37–38, 40, 43, 45–48
   truncate.py100100% 
openhands/tools
   __init__.py8275%37–38
openhands/tools/execute_bash
   __init__.py40100% 
   constants.py90100% 
   definition.py411465%65, 69–78, 148, 151, 159
   impl.py10190%38
   metadata.py502354%67–73, 77–78, 83, 85, 87–96, 100–101
openhands/tools/execute_bash/terminal
   __init__.py60100% 
   factory.py492842%24–25, 30, 32, 35, 37–38, 44–46, 74–77, 79–83, 87–89, 91, 97, 107, 111–113
   interface.py661675%43, 52, 62, 71, 76, 85, 94, 99, 104, 112, 145, 157, 162, 171, 180, 185
   subprocess_terminal.py23620313%33–34, 50–53, 56, 59–61, 67–68, 70–73, 75, 78, 80–82, 96–99, 101, 104–105, 108, 111–112, 116–117, 123–125, 127, 130–131, 133, 137–138, 140–141, 143–149, 151–157, 160–165, 167–168, 170–171, 176, 178–186, 190–192, 194–195, 197–198, 201–203, 205–208, 210–211, 213–214, 216–221, 226–228, 230, 233, 236–237, 240–241, 247–249, 251–258, 260–263, 267–275, 288–289, 291, 308–309, 312–313, 315, 317, 319–321, 324–325, 327–329, 331–332, 334–335, 344–345, 349, 351–356, 360–361, 363–366, 368–374, 376–377, 379–383, 387–388, 390–396, 400–401, 404–405, 407–408, 410–412
   terminal_session.py17813623%43, 86, 92, 96–98, 107–108, 119–121, 123–126, 135–136, 140, 144, 147–148, 150, 155, 161, 163–164, 169, 177, 182–185, 198–200, 205, 208–209, 213, 219, 233–235, 240, 243–244, 248, 255, 264, 273–274, 276, 279–283, 285, 288, 290–292, 296–297, 300–302, 306, 311–312, 316–317, 323–325, 336–337, 340–342, 344–346, 349, 359, 363, 366, 369–370, 376–377, 383, 388–389, 392–396, 402, 404–406, 412–416, 419, 422, 425–426, 428–431, 438, 442, 447–448, 455–457, 461, 465, 470–471, 475–476, 479–482, 488–489, 492
   tmux_terminal.py752764%35, 41, 85, 97–98, 100, 108–109, 111, 118, 123, 135–142, 150–151, 153–154, 156, 158–160
openhands/tools/execute_bash/utils
   command.py817211%15–19, 27, 32, 34–35, 37–38, 41–46, 48, 51–53, 55, 58–67, 74–75, 77–79, 81, 83, 90–91, 93, 95–97, 99, 101–102, 105–106, 109, 117, 120–121, 123–124, 127–129, 132–138, 141–145, 150
openhands/tools/str_replace_editor
   __init__.py30100% 
   definition.py33487%78, 145, 148, 151
   editor.py22817025%74, 76–77, 80, 99, 102, 111–115, 121–129, 131, 149–150, 170–171, 175, 179–180, 189, 193–196, 204–205, 209–211, 217, 220, 225, 228, 231–232, 235, 238–239, 243, 247, 261–263, 272, 275, 281, 286, 288–292, 294, 296, 300–301, 305–306, 314–315, 317–320, 322, 328–329, 335–337, 345–349, 353, 355–356, 363, 366, 371–372, 374, 397–398, 420–421, 423–424, 430, 433, 437–443, 446–447, 450–455, 458, 461–462, 466, 469–470, 473, 475–476, 482, 486, 504, 509–512, 514, 522, 529, 536, 547–550, 552, 554, 580–583, 592–593, 622–624, 626–635, 640–643, 656–657, 662, 667, 673, 679
   exceptions.py221340%5–6, 9, 16–18, 25–27, 38–41
   impl.py261157%31–32, 49–50, 52–54, 63–66
openhands/tools/str_replace_editor/utils
   __init__.py00100% 
   config.py20100% 
   constants.py50100% 
   encoding.py542750%42–43, 46–48, 51, 54, 60, 64–65, 67, 69, 78, 80–81, 84, 87–90, 93, 96–97, 114, 128, 130–131
   file_cache.py954849%43–46, 49–50, 54, 59, 61, 64, 69, 72–73, 87, 95–98, 108–112, 115–120, 126–130, 133–135, 138–140, 143–148, 151, 154
   history.py663645%58–60, 66–68, 70–71, 74–76, 78–79, 82, 85–86, 88, 92–94, 98–99, 102–104, 107, 111–113, 115–120, 122
   shell.py231630%30, 32–34, 38, 40, 51–55, 62–63, 70–72
openhands/tools/utils
   __init__.py00100% 
TOTAL4083136066% 

@openhands-ai

openhands-ai Bot commented Sep 6, 2025

Copy link
Copy Markdown

Summary

I have successfully fixed all failing tests on PR #143 related to the ModelResponseWithMetrics changes. Here's what was accomplished:

✅ Request Completed

🔧 Key Changes Made

  1. Added SDK Exports (openhands/sdk/__init__.py)

    • Added ModelResponseWithMetrics and Metrics to exports so tests can import them
  2. Fixed Telemetry Issue (openhands/sdk/llm/utils/telemetry.py)

    • Resolved duplicate metrics parameter error by removing existing metrics field before creating ModelResponseWithMetrics
  3. Updated All Test Files (9 files total)

    • Replaced ModelResponse with ModelResponseWithMetrics in all test mocks
    • Added metrics=Metrics() parameter to all mock responses
    • Updated imports to include the new types
  4. Fixed Test Assertions

    • Added helper functions to compare responses excluding populated metrics
    • Updated exact equality comparisons to handle real vs mock metrics data
    • Fixed tool calls test structure with proper ChatCompletionMessageToolCall objects

📊 Test Results

  • Before: Multiple test failures due to type mismatches
  • After: All 279 tests passing (LLM: 166/166, Conversation: 110/110, Integration: 3/3)

🚀 Changes Pushed

All fixes have been committed and pushed to the xw/metrics branch. The GitHub Actions should now pass successfully, allowing PR #143 to proceed with the ModelResponseWithMetrics implementation.

The changes are minimal and focused - only addressing the specific type transition without any extraneous modifications.

View full conversation

xingyaoww and others added 12 commits September 6, 2025 23:05
…ponse()

- Updated tests expecting float return from on_response() to handle Metrics object
- Changed variable names from 'cost' to 'metrics' for clarity
- Updated assertions to check metrics.accumulated_cost instead of direct float comparison
- Fixed 6 test locations that were expecting float but now receive Metrics object
- All tests now pass except for one unrelated Pydantic issue with Usage.model_extra

Co-authored-by: openhands <openhands@all-hands.dev>
- Changed test to pass cache_creation_input_tokens directly instead of in model_extra
- The Usage class constructor expects cache_creation_input_tokens as a parameter
- model_extra field doesn't have a setter and causes AttributeError
- All 261 tests now pass successfully

Co-authored-by: openhands <openhands@all-hands.dev>
- Updated mock_completion functions in both test files to handle return_metrics parameter
- Added correct Metrics import from openhands.sdk.llm.utils.metrics
- Fixed _mock_repeating_action in test_pause_functionality.py to handle return_metrics
- All 15 conversation tests now pass

Co-authored-by: openhands <openhands@all-hands.dev>
This reverts commit 80a354d.
@openhands-ai

openhands-ai Bot commented Sep 7, 2025

Copy link
Copy Markdown

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Run tests

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #143 at branch `xw/metrics`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

@xingyaoww
xingyaoww merged commit 196a486 into main Sep 7, 2025
@xingyaoww
xingyaoww deleted the xw/metrics branch September 7, 2025 15:12
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.

2 participants