Skip to content

Feature/resilience#162

Merged
tghamm merged 9 commits into
tghamm:mainfrom
Gabegi:feature/resilience
Oct 24, 2025
Merged

Feature/resilience#162
tghamm merged 9 commits into
tghamm:mainfrom
Gabegi:feature/resilience

Conversation

@Gabegi
Copy link
Copy Markdown
Contributor

@Gabegi Gabegi commented Oct 21, 2025

Hey! Sorry for the delay on this one, been swamped with other stuff.

What's this PR about?

Adds IRequestInterceptor support so you can hook into the HTTP pipeline for custom retry logic, logging, metrics, etc. - without pulling in heavy dependencies or setting up DI.

Why though?

Sometimes you just want to add some retry logic or log requests without having to wire up Microsoft.Extensions.Http.Resilience or spin up a whole DI container. This gives you a simple, lightweight way to intercept HTTP calls.

What's in here?

Core changes:

  • New IRequestInterceptor interface with proper cancellation token support
  • Added optional requestInterceptor param to AnthropicClient (fully backward compatible)
  • Wired it into BaseEndpoint's HTTP pipeline

Example implementations (ready to use or customize):

  • RetryInterceptor - exponential backoff, respects Retry-After headers, handles 408/429/5xx errors
  • LoggingInterceptor - logs requests/responses with timing, correlation IDs, and sensible size limits

Docs:

  • Moved resilience stuff to Examples section
  • Added usage examples and best practices

Quick example

var retryInterceptor = new RetryInterceptor(
    maxRetries: 3,
    initialDelay: TimeSpan.FromSeconds(1)
);

var client = new AnthropicClient(
    apiKeys: new APIAuthentication("api-key"),
    requestInterceptor: retryInterceptor
);

// Now all your requests auto-retry on transient failures
var response = await client.Messages.GetClaudeMessageAsync(parameters);

Backward compatibility?

✅ Yep, totally backward compatible. The interceptor is optional - if you don't use it, everything works exactly like before.

Testing

  • Builds across netstandard2.0, net6.0, net8.0, net9.0
  • Examples compile and work as expected

@tghamm
Copy link
Copy Markdown
Owner

tghamm commented Oct 21, 2025

@Gabegi very cool! I'll see about getting this into the sdk in the next day or so. Very much appreciated. Really like the README writeup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants