Skip to content

Conversation

@dannykopping
Copy link
Collaborator

Logs the raw HTTP requests to and responses from AI providers' APIs.

$ tree /tmp/anthropic/
/tmp/anthropic/
├── claude-haiku-4-5-20251001
│   ├── bb8e4048-8ea2-4aee-a58d-25027b962bf8.req.txt
│   ├── bb8e4048-8ea2-4aee-a58d-25027b962bf8.resp.txt
│   ├── e1f338a1-7ae9-4ccf-b898-fc3fbc25124e.req.txt
│   └── e1f338a1-7ae9-4ccf-b898-fc3fbc25124e.resp.txt
└── claude-opus-4-5-20251101
    ├── 024f344e-c75a-46ea-abfb-e45e117e121a.req.txt
    ├── 024f344e-c75a-46ea-abfb-e45e117e121a.resp.txt
    ├── 0f02d1f5-882d-474f-b83d-85f09fd63ded.req.txt
    ├── 0f02d1f5-882d-474f-b83d-85f09fd63ded.resp.txt
    ├── 7f80f624-bf0b-4c12-a521-8c7654febe5d.req.txt
    ├── 7f80f624-bf0b-4c12-a521-8c7654febe5d.resp.txt
    ├── b9a37e54-1cd2-4a4f-92aa-c9adff088f4e.req.txt
    └── b9a37e54-1cd2-4a4f-92aa-c9adff088f4e.resp.txt

@dannykopping dannykopping requested a review from pawbana December 31, 2025 12:53
@dannykopping dannykopping force-pushed the dk/request-logging-redux branch from beb1aca to d021c47 Compare January 15, 2026 13:06

return func(req *http.Request, next MiddlewareNext) (*http.Response, error) {
if err := d.dumpRequest(req); err != nil {
fmt.Fprintf(os.Stderr, "apidump: failed to dump request: %v\n", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe logger?


resp, err := next(req)
if err != nil {
return resp, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about it but maybe it be useful to have "err" file with error?

var bodyBytes []byte
if resp.Body != nil {
var err error
bodyBytes, err = io.ReadAll(resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will make streaming non responsive. I assume this is fine since logging should generally not be enabled other then when debugging.

func (i *interceptionBase) newCompletionsService(baseURL string, key string) openai.ChatCompletionService {
opts := []option.RequestOption{option.WithAPIKey(key), option.WithBaseURL(baseURL)}
func (i *interceptionBase) newCompletionsService() openai.ChatCompletionService {
opts := []option.RequestOption{option.WithAPIKey(i.cfg.Key), option.WithBaseURL(i.cfg.BaseURL)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indent

opts := []option.RequestOption{
    option.WithAPIKey(i.cfg.Key),
    option.WithBaseURL(i.cfg.BaseURL),
}

interceptor = responses.NewStreamingInterceptor(id, &req, payload, p.cfg.BaseURL, p.cfg.Key, string(req.Model))
} else {
interceptor = responses.NewBlockingInterceptor(id, &req, payload, p.baseURL, p.key, string(req.Model))
interceptor = responses.NewBlockingInterceptor(id, &req, payload, p.cfg.BaseURL, p.cfg.Key, string(req.Model))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apidump could be added to responses interceptor as well

name: config.ProviderOpenAI,
fixture: fixtures.OaiChatSimple,
providerName: config.ProviderOpenAI,
configureFunc: func(addr, dumpDir string, client aibridge.Recorder) (*aibridge.RequestBridge, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think inlined configureFunc with provider as a test param would be easier to understand

reqDumpData, err := os.ReadFile(reqDumpFile)
require.NoError(t, err, "request dump file should exist")
require.Contains(t, string(reqDumpData), "POST ")
require.Contains(t, string(reqDumpData), "Host:")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add check like require.Contains(t, string(reqDumpData), string(reqBody))

and similar for response

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants