charmbracelet/crush · error

failed to set compact mode: status code %d

Error message

failed to set compact mode: status code %d

What it means

SetCompactMode posts the compact-mode toggle to the server and expects HTTP 200. When the request completes but the server responds with any other status code, the client wraps that status in this error. It means the server explicitly rejected the compact-mode change.

Source

Thrown at internal/client/config.go:75

	defer rsp.Body.Close()
	if rsp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to update preferred model: status code %d", rsp.StatusCode)
	}
	return nil
}

// SetCompactMode sets compact mode on the server.
func (c *Client) SetCompactMode(ctx context.Context, id string, scope config.Scope, enabled bool) error {
	rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/compact", id), nil, jsonBody(struct {
		Scope   config.Scope `json:"scope"`
		Enabled bool         `json:"enabled"`
	}{Scope: scope, Enabled: enabled}), http.Header{"Content-Type": []string{"application/json"}})
	if err != nil {
		return fmt.Errorf("failed to set compact mode: %w", err)
	}
	defer rsp.Body.Close()
	if rsp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to set compact mode: status code %d", rsp.StatusCode)
	}
	return nil
}

// SetProviderAPIKey sets a provider API key on the server. The wire
// format tags the credential with an explicit Kind so the server can
// decode it back into the right Go type — JSON's `any` loses that
// information across the socket.
func (c *Client) SetProviderAPIKey(ctx context.Context, id string, scope config.Scope, providerID string, apiKey any) error {
	var (
		kind proto.APIKeyKind
		raw  json.RawMessage
	)
	switch v := apiKey.(type) {
	case string:
		kind = proto.APIKeyKindString
		b, err := json.Marshal(v)
		if err != nil {

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Log or inspect the status code embedded in the error to identify the cause
  2. Verify the workspace id passed to SetCompactMode is correct and exists
  3. Check that credentials are valid and not expired (401/403) and re-authenticate
  4. Confirm the server version supports the /config/compact endpoint

Example fix

// before
if err := client.SetCompactMode(ctx, wsID, scope, true); err != nil {
    return err
}
// after
if err := client.SetCompactMode(ctx, wsID, scope, true); err != nil {
    var statusErr *fmt.StatusError // or inspect err string for "status code"
    if errors.As(err, &statusErr) && statusErr.Code == 401 {
        if rerr := refreshAuth(ctx); rerr != nil {
            return rerr
        }
        return client.SetCompactMode(ctx, wsID, scope, true)
    }
    return err
}
Defensive patterns

Strategy: try-catch

Try / catch

if err := c.SetCompactMode(ctx, wsID, scope, enabled); err != nil {
    if strings.Contains(err.Error(), "status code 4") {
        // client-side problem: check auth and workspace id
    }
    return fmt.Errorf("compact mode: %w", err)
}

Prevention

When it happens

Trigger: Calling SetCompactMode when the server returns a non-200 response, e.g. 404 because the workspace id does not exist, 401/403 for bad credentials, or 500 for a server-side failure.

Common situations: Expired or missing auth token, wrong workspace id passed to SetCompactMode, server version that does not implement the compact-mode endpoint (404), or server error while persisting config.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/7c2cf13881efd3fa. Report an issue: GitHub.