JuliusBrussee/caveman · error

rewriter: response exceeds

Error message

rewriter: response exceeds %d bytes

What it means

The rewriter reads the provider (Anthropic/OpenAI) HTTP response body with an io.LimitReader capped at maxProviderResponseBody. If the body reaches more bytes than that cap, the response is rejected instead of being silently truncated, because truncated JSON would be unparseable and could yield bytes no gate was designed for.

Solutions

  1. Retry with a smaller prompt/output limit (max_tokens) so the provider response fits under maxProviderResponseBody
  2. Check resp headers/status first — a non-200 error page is often what is bloating the body; surface the provider error instead
  3. If legitimate responses exceed the cap, raise maxProviderResponseBody (a constant in rewriter/provider.go) and redeploy
  4. Point the client at the correct provider endpoint so it is not downloading unrelated content

Example fix

// before
raw, err := client.Complete(ctx, req) // fails: rewriter: response exceeds N bytes
// after
req.MaxTokens = clamp(req.MaxTokens, 1, maxSafeTokens) // keep reply under maxProviderResponseBody
raw, err := client.Complete(ctx, req)
if err != nil {
    var bounded *BoundedError
    if errors.As(err, &bounded) { /* shrink request and retry */ }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before calling, bound the request so the reply fits
if req.MaxTokens <= 0 || req.MaxTokens > maxProviderResponseBody/4 {
    return fmt.Errorf("max_tokens %d too large for %d-byte response cap", req.MaxTokens, maxProviderResponseBody)
}

Type guard

func isResponseTooLarge(err error) bool {
    return err != nil && strings.Contains(err.Error(), "response exceeds")
}

Try / catch

raw, err := client.Complete(ctx, req)
if err != nil {
    if isResponseTooLarge(err) {
        req.MaxTokens /= 2
        raw, err = client.Complete(ctx, req) // retry smaller
    }
    if err != nil { return fmt.Errorf("provider complete: %w", err) }
}

Prevention

When it happens

Trigger: do() in rewriter/provider.go reads the provider response with io.ReadAll(io.LimitReader(resp.Body, maxProviderResponseBody+1)); the error is returned whenever len(raw) > maxProviderResponseBody. Called from completeAnthropic and completeOpenAI, so any provider reply (including an error page or streamed dump) larger than the cap triggers it.

Common situations: A model returns an unusually long completion; the provider returns a huge error/HTML page instead of JSON; a proxy or misconfigured endpoint streams unbounded data; the cap constant is small relative to expected rewrite sizes; tests (TestProviderResponseIsBounded) exercise the bound directly.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/6cd67792a8543237. Report an issue: GitHub.

Appendix: source

Thrown at rewriter/provider.go:209

	// error rather than a panic inside the proxy hot path.
	if resp == nil {
		return nil, fmt.Errorf("rewriter: %s: nil response", endpoint)
	}
	if resp.Body == nil {
		resp.Body = http.NoBody
	}
	defer resp.Body.Close()

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		detail, _ := io.ReadAll(io.LimitReader(resp.Body, maxErrorBody))
		return nil, fmt.Errorf("rewriter: %s: status %d: %s", endpoint, resp.StatusCode, strings.TrimSpace(string(detail)))
	}
	raw, err := io.ReadAll(io.LimitReader(resp.Body, maxProviderResponseBody+1))
	if err != nil {
		return nil, fmt.Errorf("rewriter: read response: %w", err)
	}
	if len(raw) > maxProviderResponseBody {
		return nil, fmt.Errorf("rewriter: response exceeds %d bytes", maxProviderResponseBody)
	}
	return raw, nil
}

View on GitHub (pinned to 3ee70a1026)