Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Anthropic Trace

From Leeroopedia
Knowledge Sources
Domains Observability, Anthropic Protocol
Last Updated 2025-02-15 00:00 GMT

Overview

Provides trace-safe truncation and compaction utilities for Anthropic API request and response logging, ensuring large payloads are safely abbreviated without losing structural context.

Description

The trace.go file implements a set of functions that recursively truncate strings, slices, and maps in arbitrary data structures before they are written to trace logs. It enforces configurable limits on string length (240 runes), slice items (8), map entries (16), and recursion depth (4). The TraceJSON function round-trips a value through JSON marshaling and then compacts it, while TraceRequest and TraceResponse produce structured trace-safe representations of Anthropic API request and response objects.

Usage

Called internally by the Anthropic handler when trace-level logging is enabled, to safely log request and response payloads without emitting excessively large log entries for messages containing images or long text content.

Code Reference

Source Location

  • Repository: Ollama
  • File: anthropic/trace.go
  • Lines: 1-352

Signature

const (
    TraceMaxStringRunes = 240
    TraceMaxSliceItems  = 8
    TraceMaxMapEntries  = 16
    TraceMaxDepth       = 4
)

func TraceTruncateString(s string) string
func TraceJSON(v any) any
func TraceCompactValue(v any, depth int) any
func TraceRequest(ar *AnthropicRequest) map[string]any
func TraceResponse(mr *MessageResponse) map[string]any

Import

import "github.com/ollama/ollama/anthropic"

I/O Contract

Inputs

Name Type Required Description
v any Yes Any value to compact for trace output (TraceJSON, TraceCompactValue)
s string Yes String to truncate (TraceTruncateString)
ar *AnthropicRequest Yes Anthropic request to trace (TraceRequest)
mr *MessageResponse Yes Anthropic response to trace (TraceResponse)

Outputs

Name Type Description
result any Compacted, truncated representation safe for trace logging
map[string]any map Structured trace representation of request or response

Usage Examples

// Truncate a long string for logging
short := anthropic.TraceTruncateString(longBase64Image)
// "data:image/png;base64,iVBOR...(+50432 chars)"

// Create a trace-safe version of a request
traceData := anthropic.TraceRequest(anthropicReq)
slog.Debug("anthropic request", "data", traceData)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment