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

Class AttemptRecorder

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

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

Calls

no outgoing calls