llmgateway

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package llmgateway is the single chokepoint through which every LLM call in the process must pass. Its job is to bound provider concurrency (--max-concurrent-llm-requests) across every code path (direct analysis, queue-driven analysis, digest dedupe, digest summary) so the flag actually means what it says.

A Gateway owns one semaphore (`chan struct{}`) sized at construction time. Generate and Stream acquire a slot, invoke the provider, and release in a deferred call so slot leaks can't happen on panic or context cancel. One call == one slot. A 5-task analysis pipeline holds the slot 5 separate times, leaving gaps between tasks available to other callers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunIDFromContext added in v0.4.0

func RunIDFromContext(ctx context.Context) string

RunIDFromContext returns the run id tagged on ctx, or "" if none.

func WithRunID added in v0.4.0

func WithRunID(ctx context.Context, runID string) context.Context

WithRunID returns a context tagged with runID. Every gateway call made under the returned context (and its descendants, including those created by context.WithTimeout / errgroup.WithContext) is correlated to that run by the recorder. An empty runID is a no-op tag.

Types

type CallOption

type CallOption func(*callConfig)

CallOption configures a single call (Generate or Stream).

func WithLabel

func WithLabel(label string) CallOption

WithLabel attaches a human-readable label to the call for log correlation (e.g. "analyze:task=key_points", "digest:dedupe", "digest:summary").

func WithModelInfo added in v0.4.0

func WithModelInfo(providerType, modelName string) CallOption

WithModelInfo records the resolved provider type and model name for the call so the monitoring recorder can attribute tokens to a model. The gateway only sees an opaque Provider, so callers that have a resolved selection pass it here.

type CallRecord added in v0.4.0

type CallRecord struct {
	RunID        string
	Label        string
	ProviderType string
	ModelName    string
	Prompt       string
	Response     string
	Usage        llmprovider.Usage
	UsageKnown   bool
	Duration     time.Duration
	Err          error
}

CallRecord is one completed LLM call handed to a Recorder. The gateway fills the fields it knows; provider/model come from WithModelInfo, the run id from the call context (see WithRunID).

type Gateway

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

Gateway throttles LLM calls globally.

func New

func New(maxConcurrent int) *Gateway

New creates a Gateway that allows at most maxConcurrent simultaneous LLM calls. maxConcurrent < 1 is coerced to 1.

func (*Gateway) Generate

func (g *Gateway) Generate(
	ctx context.Context,
	p llmprovider.Provider,
	prompt string,
	opts ...CallOption,
) (string, error)

Generate makes a one-shot (non-streaming) LLM call through the gateway.

func (*Gateway) MaxConcurrent

func (g *Gateway) MaxConcurrent() int

MaxConcurrent returns the configured cap.

func (*Gateway) SetRecorder added in v0.4.0

func (g *Gateway) SetRecorder(r Recorder)

SetRecorder installs r as the gateway's call recorder. Passing nil disables recording. Not safe to call concurrently with in-flight calls; set once at construction.

func (*Gateway) Stats

func (g *Gateway) Stats() Stats

Stats returns a snapshot of runtime counters.

func (*Gateway) Stream

func (g *Gateway) Stream(
	ctx context.Context,
	p llmprovider.ChatModelProvider,
	messages []*schema.Message,
	onChunk func(chunk *schema.Message) error,
	opts ...CallOption,
) (resp string, err error)

Stream opens a streaming LLM call through the gateway, drains the reader, and invokes onChunk for each message chunk as it arrives. The slot is held for the entire duration of the stream (reader open → io.EOF / error / cancel).

onChunk may be nil; in that case chunks are still accumulated and returned as the full response string, but no per-token callback is invoked. Returning a non-nil error from onChunk aborts the stream and is returned.

type Recorder added in v0.4.0

type Recorder interface {
	Record(CallRecord)
}

Recorder receives every LLM call that passes through the gateway. It lets the monitoring layer persist calls without the gateway depending on the store or models packages (avoiding an import cycle). Record must not block.

type Stats

type Stats struct {
	MaxConcurrent int
	InFlight      int64
	Waiting       int64
	TotalCalls    int64
}

Stats is a snapshot of the gateway's counters.

Jump to

Keyboard shortcuts

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