MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / AttemptRecorder

Class AttemptRecorder

tests/test_middleware.py:196–224  ·  view source on GitHub ↗

Records the `retries_taken` and response status of every attempt the chain runs for.

Source from the content-addressed store, hash-verified

194
195
196class AttemptRecorder(Middleware):
197 """Records the `retries_taken` and response status of every attempt the chain runs for."""
198
199 def __init__(self) -> None:
200 self.attempts: list[int] = []
201 self.statuses: list[int] = []
202 self.errors: list[Exception] = []
203
204 @override
205 def handle(self, request: APIRequest, call_next: CallNext) -> Any:
206 self.attempts.append(request.retries_taken)
207 try:
208 response = call_next(request)
209 except Exception as err:
210 self.errors.append(err)
211 raise
212 self.statuses.append(response.status_code)
213 return response
214
215 @override
216 async def handle_async(self, request: APIRequest, call_next: AsyncCallNext) -> Any:
217 self.attempts.append(request.retries_taken)
218 try:
219 response = await call_next(request)
220 except Exception as err:
221 self.errors.append(err)
222 raise
223 self.statuses.append(response.status_code)
224 return response
225
226
227class Boom(Exception):

Calls

no outgoing calls