llm

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCircuitOpen = fmt.Errorf("llm: circuit breaker open — too many recent failures")

ErrCircuitOpen is returned by ResilientClient when the circuit breaker is open.

Functions

func StripCodeFences

func StripCodeFences(s string) string

StripCodeFences removes markdown code fences (```json ... ```) that some models wrap around JSON responses. Returns the original string if no fences found.

Types

type AnthropicClient

type AnthropicClient struct {
	// contains filtered or unexported fields
}

AnthropicClient wraps the Anthropic Go SDK and implements LLMClient.

func NewAnthropicClient

func NewAnthropicClient(apiKey string) *AnthropicClient

NewAnthropicClient creates an AnthropicClient authenticated with apiKey.

func (*AnthropicClient) Complete

func (a *AnthropicClient) Complete(ctx context.Context, model, systemPrompt, userMessage string, maxTokens int) (string, error)

Complete sends a single-turn request to the Anthropic Messages API and returns the first text block from the response.

type GatewayClient

type GatewayClient struct {
	// contains filtered or unexported fields
}

GatewayClient sends completions through an OpenAI-compatible HTTP gateway. It implements LLMClient.

func NewGatewayClient

func NewGatewayClient(baseURL, token string, timeoutSeconds int) *GatewayClient

NewGatewayClient creates a GatewayClient that POSTs to baseURL/v1/chat/completions authenticated with token. timeoutSeconds controls the HTTP client timeout (0 = no timeout).

func (*GatewayClient) Complete

func (g *GatewayClient) Complete(ctx context.Context, model, systemPrompt, userMessage string, maxTokens int) (string, error)

Complete sends a single-turn request to the gateway and returns the model reply.

type HTTPError added in v0.10.0

type HTTPError struct {
	StatusCode int
	Body       string
}

HTTPError is returned when an HTTP gateway responds with a non-2xx status.

func (*HTTPError) Error added in v0.10.0

func (e *HTTPError) Error() string

Error implements the error interface.

type LLMClient

type LLMClient interface {
	Complete(ctx context.Context, model, systemPrompt, userMessage string, maxTokens int) (string, error)
}

LLMClient is the interface for sending a single-turn completion to a language model. All implementations must be safe for concurrent use.

func NewClient

func NewClient(cfg config.ClaudeConfig) LLMClient

NewClient returns a ResilientClient wrapping the appropriate concrete LLMClient, or nil if no credentials are configured.

Priority:

  1. GatewayURL + GatewayToken set → GatewayClient (OpenAI-compatible gateway)
  2. APIKey set → AnthropicClient (direct Anthropic SDK)
  3. Neither set → nil (no LLM available; callers must guard against this)

type ResilientClient added in v0.10.0

type ResilientClient struct {
	// contains filtered or unexported fields
}

ResilientClient wraps any LLMClient with a circuit breaker, exponential backoff retry, and a worker pool concurrency cap.

func NewResilientClient added in v0.10.0

func NewResilientClient(
	inner LLMClient,
	maxConcurrent, cbFailureThreshold, cbRecoverySeconds, maxRetries int,
) *ResilientClient

NewResilientClient wraps inner with resilience controls.

  • maxConcurrent: maximum simultaneous in-flight LLM calls (worker pool size)
  • cbFailureThreshold: consecutive failures before the circuit opens
  • cbRecoverySeconds: seconds the circuit stays open before allowing a probe
  • maxRetries: maximum additional attempts per call (0 = try once, no retry)

func (*ResilientClient) Complete added in v0.10.0

func (r *ResilientClient) Complete(
	ctx context.Context,
	model, systemPrompt, userMessage string,
	maxTokens int,
) (string, error)

Complete satisfies the LLMClient interface. It acquires a worker pool slot, then delegates to the circuit breaker which wraps the retry loop.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL