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

Class AttemptRecorder

tests/test_middleware.py:198–226  ·  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

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

Calls

no outgoing calls