|
| 1 | +# Implementation Plan |
| 2 | + |
| 3 | +- [x] 1. Create PricingService infrastructure |
| 4 | + - Create `agentle/responses/pricing/pricing_service.py` with abstract PricingService class |
| 5 | + - Create `agentle/responses/pricing/default_pricing_service.py` with static pricing dictionary |
| 6 | + - Create `agentle/responses/pricing/__init__.py` to export classes |
| 7 | + - _Requirements: 2.2, 2.3_ |
| 8 | + |
| 9 | +- [x] 2. Implement tracing helper methods in Responder |
| 10 | +- [x] 2.1 Implement `_prepare_trace_input_data` method |
| 11 | + - Extract relevant fields from request_payload (input, model, tools, reasoning, etc.) |
| 12 | + - Return structured dictionary for trace context |
| 13 | + - _Requirements: 5.1, 5.3, 5.4_ |
| 14 | + |
| 15 | +- [x] 2.2 Implement `_prepare_trace_metadata` method |
| 16 | + - Extract model, provider, base_url |
| 17 | + - Merge custom metadata from generation_config |
| 18 | + - Return metadata dictionary |
| 19 | + - _Requirements: 5.1, 5.2_ |
| 20 | + |
| 21 | +- [x] 2.3 Implement `_extract_usage_from_response` method |
| 22 | + - Extract token usage from Response object |
| 23 | + - Handle missing usage data gracefully |
| 24 | + - Return usage dictionary with input/output/total tokens |
| 25 | + - _Requirements: 2.1, 2.5_ |
| 26 | + |
| 27 | +- [x] 2.4 Implement `_calculate_costs` method |
| 28 | + - Use PricingService to get model pricing |
| 29 | + - Calculate input and output costs |
| 30 | + - Return cost dictionary with breakdown |
| 31 | + - Handle unknown models gracefully |
| 32 | + - _Requirements: 2.2, 2.3, 2.4_ |
| 33 | + |
| 34 | +- [x] 3. Implement tracing context management methods |
| 35 | +- [x] 3.1 Implement `_create_tracing_contexts` method |
| 36 | + - Iterate through all otel_clients |
| 37 | + - Create trace context for each client |
| 38 | + - Create generation context for each client |
| 39 | + - Store contexts in list of dictionaries |
| 40 | + - Handle context creation errors gracefully |
| 41 | + - _Requirements: 1.2, 1.5, 4.1, 4.2, 4.3_ |
| 42 | + |
| 43 | +- [x] 3.2 Implement `_update_tracing_success` method |
| 44 | + - Extract usage from response |
| 45 | + - Calculate costs using PricingService |
| 46 | + - Update generation contexts with output, usage, and costs |
| 47 | + - Update trace contexts with success status and metadata |
| 48 | + - Handle structured output parsing |
| 49 | + - Use fire-and-forget for non-critical operations |
| 50 | + - _Requirements: 1.3, 2.1, 2.2, 2.3, 2.4, 4.4, 5.2, 5.3, 5.5_ |
| 51 | + |
| 52 | +- [x] 3.3 Implement `_update_tracing_error` method |
| 53 | + - Update generation contexts with error information |
| 54 | + - Update trace contexts with failure status |
| 55 | + - Handle errors in error handling gracefully |
| 56 | + - _Requirements: 1.4, 4.1, 4.2, 4.3_ |
| 57 | + |
| 58 | +- [x] 3.4 Implement `_cleanup_tracing_contexts` method |
| 59 | + - Close all generation context generators |
| 60 | + - Close all trace context generators |
| 61 | + - Handle cleanup errors gracefully |
| 62 | + - _Requirements: 3.3, 4.5_ |
| 63 | + |
| 64 | +- [x] 4. Integrate tracing into non-streaming flow |
| 65 | +- [x] 4.1 Modify `_respond_async` method for non-streaming |
| 66 | + - Add start_time tracking |
| 67 | + - Initialize active_contexts list |
| 68 | + - Call `_create_tracing_contexts` if otel_clients present |
| 69 | + - Wrap API call in try-except-finally |
| 70 | + - Call `_update_tracing_success` on success |
| 71 | + - Call `_update_tracing_error` on error |
| 72 | + - Call `_cleanup_tracing_contexts` in finally block |
| 73 | + - _Requirements: 1.1, 1.2, 1.3, 1.4, 4.5, 6.1, 6.2, 6.3, 6.4, 6.5_ |
| 74 | + |
| 75 | +- [x] 5. Integrate tracing into streaming flow |
| 76 | +- [x] 5.1 Create `_stream_events_with_tracing` wrapper method |
| 77 | + - Accept content_lines, text_format, active_contexts, start_time, model |
| 78 | + - Wrap `_stream_events_from_buffer` generator |
| 79 | + - Accumulate text deltas for metrics |
| 80 | + - Track final ResponseCompletedEvent |
| 81 | + - Call `_update_tracing_success` on completion |
| 82 | + - Call `_update_tracing_error` on error |
| 83 | + - _Requirements: 3.1, 3.2, 3.3, 6.1, 6.2, 6.3, 6.4, 6.5_ |
| 84 | + |
| 85 | +- [x] 5.2 Modify `_respond_async` method for streaming |
| 86 | + - Create tracing contexts before streaming |
| 87 | + - Pass contexts to `_stream_events_with_tracing` |
| 88 | + - Ensure cleanup happens after streaming completes |
| 89 | + - _Requirements: 3.1, 3.3, 4.5_ |
| 90 | + |
| 91 | +- [x] 6. Add PricingService to Responder initialization |
| 92 | +- [x] 6.1 Add `pricing_service` parameter to `__init__` |
| 93 | + - Make it optional with default DefaultPricingService |
| 94 | + - Store as instance attribute |
| 95 | + - _Requirements: 2.2, 2.3_ |
| 96 | + |
| 97 | +- [x] 6.2 Update `from_openrouter` and `from_openai` class methods |
| 98 | + - Pass pricing_service parameter through |
| 99 | + - _Requirements: 2.2, 2.3_ |
| 100 | + |
| 101 | +- [x] 7. Add logging and error handling |
| 102 | +- [x] 7.1 Add debug logging for tracing operations |
| 103 | + - Log context creation |
| 104 | + - Log context updates |
| 105 | + - Log context cleanup |
| 106 | + - _Requirements: 4.1, 4.2, 4.3_ |
| 107 | + |
| 108 | +- [x] 7.2 Add error logging for tracing failures |
| 109 | + - Log context creation failures |
| 110 | + - Log update failures |
| 111 | + - Log cleanup failures |
| 112 | + - Ensure errors don't propagate to caller |
| 113 | + - _Requirements: 4.1, 4.2, 4.3, 4.4_ |
| 114 | + |
| 115 | +- [x] 8. Update type hints and imports |
| 116 | +- [x] 8.1 Add necessary imports |
| 117 | + - Import datetime |
| 118 | + - Import fire_and_forget from rsb.coroutines |
| 119 | + - Import OtelClient types |
| 120 | + - Import PricingService |
| 121 | + - _Requirements: All_ |
| 122 | + |
| 123 | +- [x] 8.2 Update type hints |
| 124 | + - Add type hints to all new methods |
| 125 | + - Ensure consistency with existing code style |
| 126 | + - _Requirements: All_ |
| 127 | + |
| 128 | +- [x] 9. Documentation and examples |
| 129 | +- [x] 9.1 Add docstrings to all new methods |
| 130 | + - Document parameters |
| 131 | + - Document return values |
| 132 | + - Document error handling behavior |
| 133 | + - _Requirements: All_ |
| 134 | + |
| 135 | +- [x] 9.2 Update Responder class docstring |
| 136 | + - Document otel_clients parameter |
| 137 | + - Document pricing_service parameter |
| 138 | + - Document tracing behavior |
| 139 | + - _Requirements: All_ |
| 140 | + |
| 141 | +- [x] 9.3 Create usage example |
| 142 | + - Create example showing Responder with OtelClient |
| 143 | + - Show both streaming and non-streaming usage |
| 144 | + - Demonstrate cost tracking |
| 145 | + - _Requirements: All_ |
0 commit comments