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

Class AttemptRecorder

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

222
223
224class AttemptRecorder(Middleware):
225 """Records the `retries_taken` and response status of every attempt the chain runs for."""
226
227 def __init__(self) -> None:
228 self.attempts: list[int] = []
229 self.statuses: list[int] = []
230 self.errors: list[Exception] = []
231
232 @override
233 def handle(self, request: APIRequest, call_next: CallNext) -> Any:
234 self.attempts.append(request.retries_taken)
235 try:
236 response = call_next(request)
237 except Exception as err:
238 self.errors.append(err)
239 raise
240 self.statuses.append(response.status_code)
241 return response
242
243 @override
244 async def handle_async(self, request: APIRequest, call_next: AsyncCallNext) -> Any:
245 self.attempts.append(request.retries_taken)
246 try:
247 response = await call_next(request)
248 except Exception as err:
249 self.errors.append(err)
250 raise
251 self.statuses.append(response.status_code)
252 return response
253
254
255class Boom(Exception):

Calls

no outgoing calls