ldai

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package ldai contains an AI SDK suitable for usage with generative AI applications.

Index

Constants

View Source
const (
	JudgePlaceholderMessageHistory     = "{{message_history}}"
	JudgePlaceholderResponseToEvaluate = "{{response_to_evaluate}}"
)

JudgePlaceholderMessageHistory and JudgePlaceholderResponseToEvaluate are the literal placeholder strings injected during judge config evaluation (pass 1) and consumed by Judge.buildMessages (pass 2). Both passes must use the same values or substitution silently fails.

View Source
const (
	// Version is the current version string of the ldai package. This is updated by our release scripts.
	Version = "0.10.0" // {{ x-release-please-version }}

	// SDKName is the canonical name of this AI SDK package.
	SDKName = "go-server-sdk-ai"

	// SDKLanguage is the programming language of this AI SDK.
	SDKLanguage = "go"
)

Variables

This section is empty.

Functions

func TrackMetricsOf

func TrackMetricsOf[T any](t *Tracker, extract func(T) AIMetrics, operation func() (T, error)) (T, error)

TrackMetricsOf runs operation, calls extract to get AIMetrics from the result, then tracks duration (runner-reported DurationMs if set, otherwise wall-clock), success/error, and tokens. Valid for completion, agent, and judge trackers.

Types

type AIAgentConfig

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

AIAgentConfig represents an agent-mode AI Config retrieved from LaunchDarkly. It provides access to model parameters, provider details, agent instructions, tools, and judge configuration. Use Client.AgentConfig to obtain an instance.

To send analytic events to LaunchDarkly, call CreateTracker to obtain a Tracker.

func (*AIAgentConfig) CreateTracker

func (b *AIAgentConfig) CreateTracker() *Tracker

CreateTracker returns a new Tracker for a fresh AI run.

Returns nil if the config was not obtained via the Client.

func (*AIAgentConfig) Enabled

func (b *AIAgentConfig) Enabled() bool

Enabled returns whether the config is enabled.

func (*AIAgentConfig) Instructions

func (c *AIAgentConfig) Instructions() string

Instructions returns the agent's system instructions string.

func (*AIAgentConfig) JudgeConfiguration

func (c *AIAgentConfig) JudgeConfiguration() *datamodel.JudgeConfiguration

JudgeConfiguration returns the judge configuration attached to this config, if any. Returns a defensive copy to prevent mutations.

func (*AIAgentConfig) Key

func (b *AIAgentConfig) Key() string

Key returns the feature flag key used to retrieve this config.

func (*AIAgentConfig) Model

func (b *AIAgentConfig) Model() ModelConfig

Model returns a defensive copy of the model configuration.

func (*AIAgentConfig) ModelName

func (b *AIAgentConfig) ModelName() string

ModelName returns the model name associated with the config.

func (*AIAgentConfig) Provider

func (b *AIAgentConfig) Provider() ProviderConfig

Provider returns the provider configuration.

func (*AIAgentConfig) ProviderName

func (b *AIAgentConfig) ProviderName() string

ProviderName returns the provider name associated with the config.

func (*AIAgentConfig) Tools

func (b *AIAgentConfig) Tools() map[string]ToolConfig

Tools returns a defensive copy of the root-level tools map. This is distinct from model.parameters.tools which is passed to LLM providers verbatim.

type AIAgentConfigDefault

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

AIAgentConfigDefault is the user-constructed default for AgentConfig calls. It is returned when LaunchDarkly is unreachable or the flag cannot be evaluated. By default the config is enabled; use Disabled to obtain a disabled default.

func NewAIAgentConfigDefault

func NewAIAgentConfigDefault() AIAgentConfigDefault

NewAIAgentConfigDefault returns a new enabled AIAgentConfigDefault.

func (AIAgentConfigDefault) AsLdValue

func (d AIAgentConfigDefault) AsLdValue() ldvalue.Value

AsLdValue serializes this default as an ldvalue.Value for use as a JSONVariation fallback.

func (AIAgentConfigDefault) Disabled

Disabled returns a copy of this default with enabled set to false.

func (AIAgentConfigDefault) WithCustomModelParam

func (d AIAgentConfigDefault) WithCustomModelParam(key string, value ldvalue.Value) AIAgentConfigDefault

WithCustomModelParam sets a custom model parameter.

func (AIAgentConfigDefault) WithEnabled

func (d AIAgentConfigDefault) WithEnabled(enabled bool) AIAgentConfigDefault

WithEnabled sets whether the default config is enabled.

func (AIAgentConfigDefault) WithInstructions

func (d AIAgentConfigDefault) WithInstructions(instructions string) AIAgentConfigDefault

WithInstructions sets the agent's system instructions.

func (AIAgentConfigDefault) WithJudgeConfiguration

WithJudgeConfiguration sets the judge configuration. The provided value is defensively copied.

func (AIAgentConfigDefault) WithModelName

func (d AIAgentConfigDefault) WithModelName(name string) AIAgentConfigDefault

WithModelName sets the model name.

func (AIAgentConfigDefault) WithModelParam

func (d AIAgentConfigDefault) WithModelParam(key string, value ldvalue.Value) AIAgentConfigDefault

WithModelParam sets a model parameter.

func (AIAgentConfigDefault) WithProviderName

func (d AIAgentConfigDefault) WithProviderName(name string) AIAgentConfigDefault

WithProviderName sets the provider name.

func (AIAgentConfigDefault) WithTool

WithTool adds a tool definition to the default. The tool is keyed by its Name field. The provided tool is used as-is; call this multiple times to add multiple tools.

type AICompletionConfig

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

AICompletionConfig represents a completion-mode AI Config retrieved from LaunchDarkly. It provides access to model parameters, provider details, messages, tools, and judge configuration. Use Client.CompletionConfig to obtain an instance.

To send analytic events to LaunchDarkly, call CreateTracker to obtain a Tracker.

func (*AICompletionConfig) CreateTracker

func (b *AICompletionConfig) CreateTracker() *Tracker

CreateTracker returns a new Tracker for a fresh AI run.

Returns nil if the config was not obtained via the Client.

func (*AICompletionConfig) CustomModelParam deprecated

func (c *AICompletionConfig) CustomModelParam(key string) (ldvalue.Value, bool)

CustomModelParam returns the custom model parameter named by key. The second return value is true if the key exists.

Deprecated: Use Model().Custom instead.

func (*AICompletionConfig) Enabled

func (b *AICompletionConfig) Enabled() bool

Enabled returns whether the config is enabled.

func (*AICompletionConfig) EvaluationMetricKey deprecated

func (c *AICompletionConfig) EvaluationMetricKey() string

EvaluationMetricKey returns the evaluation metric key for judge mode configs.

Deprecated: Use AIJudgeConfig.EvaluationMetricKey instead.

func (*AICompletionConfig) EvaluationMetricKeys deprecated

func (c *AICompletionConfig) EvaluationMetricKeys() []string

EvaluationMetricKeys returns the deprecated array of evaluation metric keys.

Deprecated: Use EvaluationMetricKey instead.

func (*AICompletionConfig) JudgeConfiguration

func (c *AICompletionConfig) JudgeConfiguration() *datamodel.JudgeConfiguration

JudgeConfiguration returns the judge configuration attached to this config, if any.

func (*AICompletionConfig) Key

func (b *AICompletionConfig) Key() string

Key returns the feature flag key used to retrieve this config.

func (*AICompletionConfig) Messages

func (c *AICompletionConfig) Messages() []datamodel.Message

Messages returns the interpolated messages defined by the config. The series of messages may be passed to an AI model provider.

func (*AICompletionConfig) Mode deprecated

func (c *AICompletionConfig) Mode() string

Mode returns the AI Config mode (e.g., "completion", "agent", "judge").

Deprecated: The config type itself indicates the mode.

func (*AICompletionConfig) Model

func (b *AICompletionConfig) Model() ModelConfig

Model returns a defensive copy of the model configuration.

func (*AICompletionConfig) ModelName deprecated

func (c *AICompletionConfig) ModelName() string

ModelName returns the model name associated with the config.

Deprecated: Use Model().Name instead.

func (*AICompletionConfig) ModelParam deprecated

func (c *AICompletionConfig) ModelParam(key string) (ldvalue.Value, bool)

ModelParam returns the model parameter named by key. The second return value is true if the key exists.

Deprecated: Use Model().Parameters instead.

func (*AICompletionConfig) Provider

func (b *AICompletionConfig) Provider() ProviderConfig

Provider returns the provider configuration.

func (*AICompletionConfig) ProviderName deprecated

func (c *AICompletionConfig) ProviderName() string

ProviderName returns the provider name associated with the config.

Deprecated: Use Provider().Name instead.

func (*AICompletionConfig) Tools

func (b *AICompletionConfig) Tools() map[string]ToolConfig

Tools returns a defensive copy of the root-level tools map. This is distinct from model.parameters.tools which is passed to LLM providers verbatim.

func (*AICompletionConfig) VariationKey deprecated

func (c *AICompletionConfig) VariationKey() string

VariationKey is used internally by LaunchDarkly.

Deprecated: This is an internal implementation detail.

func (*AICompletionConfig) Version deprecated

func (c *AICompletionConfig) Version() int

Version is used internally by LaunchDarkly.

Deprecated: This is an internal implementation detail.

type AICompletionConfigDefault

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

AICompletionConfigDefault is the user-constructed default for CompletionConfig calls. It is returned when LaunchDarkly is unreachable or the flag cannot be evaluated. By default the config is enabled; use Disabled to obtain a disabled default.

func Disabled deprecated

func Disabled() AICompletionConfigDefault

Disabled returns an AICompletionConfigDefault that is disabled and contains no messages. It is a convenience constructor equivalent to NewAICompletionConfigDefault().Disabled().

Deprecated: Use NewAICompletionConfigDefault().Disabled() instead.

func NewAICompletionConfigDefault

func NewAICompletionConfigDefault() AICompletionConfigDefault

NewAICompletionConfigDefault returns a new enabled AICompletionConfigDefault.

func (AICompletionConfigDefault) AsLdValue

func (d AICompletionConfigDefault) AsLdValue() ldvalue.Value

AsLdValue serializes this default as an ldvalue.Value for use as a JSONVariation fallback.

func (AICompletionConfigDefault) Disabled

Disabled returns a copy of this default with enabled set to false.

func (AICompletionConfigDefault) WithCustomModelParam

func (d AICompletionConfigDefault) WithCustomModelParam(key string, value ldvalue.Value) AICompletionConfigDefault

WithCustomModelParam sets a custom model parameter.

func (AICompletionConfigDefault) WithEnabled

WithEnabled sets whether the default config is enabled.

func (AICompletionConfigDefault) WithJudgeConfiguration

WithJudgeConfiguration sets the judge configuration. The provided value is defensively copied.

func (AICompletionConfigDefault) WithMessage

WithMessage appends a message with the given content and role.

func (AICompletionConfigDefault) WithModelName

WithModelName sets the model name.

func (AICompletionConfigDefault) WithModelParam

WithModelParam sets a model parameter.

func (AICompletionConfigDefault) WithProviderName

WithProviderName sets the provider name.

func (AICompletionConfigDefault) WithTool

WithTool adds a tool definition to the default. The tool is keyed by its Name field. Call this multiple times to add multiple tools.

type AIJudgeConfig

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

AIJudgeConfig represents a judge-mode AI Config retrieved from LaunchDarkly. It provides access to model parameters, provider details, judge messages, and the evaluation metric key. Use Client.JudgeConfig to obtain an instance.

To send analytic events to LaunchDarkly, call CreateTracker to obtain a Tracker.

func (*AIJudgeConfig) CreateTracker

func (b *AIJudgeConfig) CreateTracker() *Tracker

CreateTracker returns a new Tracker for a fresh AI run.

Returns nil if the config was not obtained via the Client.

func (*AIJudgeConfig) Enabled

func (b *AIJudgeConfig) Enabled() bool

Enabled returns whether the config is enabled.

func (*AIJudgeConfig) EvaluationMetricKey

func (c *AIJudgeConfig) EvaluationMetricKey() string

EvaluationMetricKey returns the evaluation metric key used to record judge scores.

func (*AIJudgeConfig) Key

func (b *AIJudgeConfig) Key() string

Key returns the feature flag key used to retrieve this config.

func (*AIJudgeConfig) Messages

func (c *AIJudgeConfig) Messages() []datamodel.Message

Messages returns the interpolated judge messages. The messages may contain placeholder strings for the message history and response to evaluate, resolved during evaluation.

func (*AIJudgeConfig) Model

func (b *AIJudgeConfig) Model() ModelConfig

Model returns a defensive copy of the model configuration.

func (*AIJudgeConfig) ModelName

func (b *AIJudgeConfig) ModelName() string

ModelName returns the model name associated with the config.

func (*AIJudgeConfig) Provider

func (b *AIJudgeConfig) Provider() ProviderConfig

Provider returns the provider configuration.

func (*AIJudgeConfig) ProviderName

func (b *AIJudgeConfig) ProviderName() string

ProviderName returns the provider name associated with the config.

func (*AIJudgeConfig) Tools

func (b *AIJudgeConfig) Tools() map[string]ToolConfig

Tools returns a defensive copy of the root-level tools map. This is distinct from model.parameters.tools which is passed to LLM providers verbatim.

type AIJudgeConfigDefault

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

AIJudgeConfigDefault is the user-constructed default for JudgeConfig calls. It is returned when LaunchDarkly is unreachable or the flag cannot be evaluated. By default the config is enabled; use Disabled to obtain a disabled default.

func NewAIJudgeConfigDefault

func NewAIJudgeConfigDefault() AIJudgeConfigDefault

NewAIJudgeConfigDefault returns a new enabled AIJudgeConfigDefault.

func (AIJudgeConfigDefault) AsLdValue

func (d AIJudgeConfigDefault) AsLdValue() ldvalue.Value

AsLdValue serializes this default as an ldvalue.Value for use as a JSONVariation fallback.

func (AIJudgeConfigDefault) Disabled

Disabled returns a copy of this default with enabled set to false.

func (AIJudgeConfigDefault) WithCustomModelParam

func (d AIJudgeConfigDefault) WithCustomModelParam(key string, value ldvalue.Value) AIJudgeConfigDefault

WithCustomModelParam sets a custom model parameter.

func (AIJudgeConfigDefault) WithEnabled

func (d AIJudgeConfigDefault) WithEnabled(enabled bool) AIJudgeConfigDefault

WithEnabled sets whether the default config is enabled.

func (AIJudgeConfigDefault) WithEvaluationMetricKey

func (d AIJudgeConfigDefault) WithEvaluationMetricKey(key string) AIJudgeConfigDefault

WithEvaluationMetricKey sets the evaluation metric key.

func (AIJudgeConfigDefault) WithMessage

func (d AIJudgeConfigDefault) WithMessage(content string, role datamodel.Role) AIJudgeConfigDefault

WithMessage appends a message with the given content and role.

func (AIJudgeConfigDefault) WithModelName

func (d AIJudgeConfigDefault) WithModelName(name string) AIJudgeConfigDefault

WithModelName sets the model name.

func (AIJudgeConfigDefault) WithModelParam

func (d AIJudgeConfigDefault) WithModelParam(key string, value ldvalue.Value) AIJudgeConfigDefault

WithModelParam sets a model parameter.

func (AIJudgeConfigDefault) WithProviderName

func (d AIJudgeConfigDefault) WithProviderName(name string) AIJudgeConfigDefault

WithProviderName sets the provider name.

type AIMetrics

type AIMetrics struct {
	// Success indicates whether the operation succeeded.
	Success bool
	// Tokens is the optional token usage to record.
	Tokens *TokenUsage
	// DurationMs is an optional runner-reported duration override in milliseconds.
	// When nil, TrackMetricsOf uses the measured wall-clock duration instead.
	DurationMs *float64
}

AIMetrics is the mode-agnostic metrics summary returned by an operation passed to TrackMetricsOf.

type AgentConfigRequest

type AgentConfigRequest struct {
	Key          string
	DefaultValue AIAgentConfigDefault
	Variables    map[string]interface{}
}

AgentConfigRequest pairs a flag key with its per-agent default and interpolation variables for use with AgentConfigs. Each request is evaluated independently.

type AgentGraphDefinition

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

AgentGraphDefinition is a fully resolved agent graph returned by Client.AgentGraph.

When Enabled returns false, the graph was not fetchable or failed validation; node collections are empty and traversal methods are no-ops. CreateTracker remains available on disabled graphs so callers can still record an invocation failure.

func (*AgentGraphDefinition) CreateTracker

func (d *AgentGraphDefinition) CreateTracker() *GraphTracker

CreateTracker returns a new GraphTracker for a fresh graph invocation. Returns nil if no tracker factory was wired (for example, a blank graph key).

func (*AgentGraphDefinition) Enabled

func (d *AgentGraphDefinition) Enabled() bool

Enabled reports whether the graph passed validation and all node configs were fetched.

func (*AgentGraphDefinition) GetChildNodes

func (d *AgentGraphDefinition) GetChildNodes(nodeKey string) []*AgentGraphNode

GetChildNodes returns the immediate children of the node with the given key by following its outgoing edges. Missing targets are skipped. Returns nil if the node is unknown.

func (*AgentGraphDefinition) GetNode

func (d *AgentGraphDefinition) GetNode(nodeKey string) *AgentGraphNode

GetNode returns the node with the given key, or nil if not found.

func (*AgentGraphDefinition) GetParentNodes

func (d *AgentGraphDefinition) GetParentNodes(nodeKey string) []*AgentGraphNode

GetParentNodes returns nodes with an outgoing edge to nodeKey, in graph encounter order.

func (*AgentGraphDefinition) ReverseTraverse

func (d *AgentGraphDefinition) ReverseTraverse(fn TraverseFunc, initialContext map[string]interface{})

ReverseTraverse visits reachable nodes in reverse dependency order (root last). Non-root nodes are released by out-degree (Kahn); on cycles among non-root nodes the unvisited non-root with the lowest remaining out-degree is chosen next, with ties broken by discovery order. Each callback receives a fresh context containing initialContext plus only that node's descendant results. initialContext is never mutated. No-op when the graph has no root or fn is nil. Pure cycles still visit every node (root last).

func (*AgentGraphDefinition) RootNode

func (d *AgentGraphDefinition) RootNode() *AgentGraphNode

RootNode returns the root node, or nil if the graph is disabled or the root is absent.

func (*AgentGraphDefinition) TerminalNodes

func (d *AgentGraphDefinition) TerminalNodes() []*AgentGraphNode

TerminalNodes returns nodes with no outgoing edges, in graph encounter order.

func (*AgentGraphDefinition) Traverse

func (d *AgentGraphDefinition) Traverse(fn TraverseFunc, initialContext map[string]interface{})

Traverse visits reachable nodes in dependency (topological) order starting from the root. Each node is visited at most once. On cycles, the unvisited node with the lowest remaining in-degree is chosen next, with ties broken by discovery order (nodeKeys). Each callback receives a fresh context containing initialContext plus only that node's dependency results. initialContext is never mutated. No-op when the graph has no root or fn is nil.

type AgentGraphNode

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

AgentGraphNode is a single node in a resolved agent graph.

func (*AgentGraphNode) Config

func (n *AgentGraphNode) Config() *AIAgentConfig

Config returns the agent config for this node.

func (*AgentGraphNode) Edges

func (n *AgentGraphNode) Edges() []GraphEdge

Edges returns a copy of the outgoing edges from this node.

func (*AgentGraphNode) IsTerminal

func (n *AgentGraphNode) IsTerminal() bool

IsTerminal reports whether this node has no outgoing edges.

func (*AgentGraphNode) Key

func (n *AgentGraphNode) Key() string

Key returns the node config key.

type Client

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

Client is the main entrypoint for the AI SDK. A client can be used to obtain an AI Config from LaunchDarkly. Unless otherwise noted, the Client's method are not safe for concurrent use.

func NewClient

func NewClient(sdk ServerSDK) (*Client, error)

NewClient creates a new AI Client. The provided SDK interface must not be nil. The client will use the provided SDK's loggers to log warnings and errors.

func (*Client) AgentConfig

func (c *Client) AgentConfig(
	key string,
	context ldcontext.Context,
	defaultValue AIAgentConfigDefault,
	variables map[string]interface{},
) AIAgentConfig

AgentConfig retrieves an AI agent config and interpolates its instruction template using the provided variables. Returns the default value if the config cannot be evaluated.

To send analytic events to LaunchDarkly, call CreateTracker on the returned AIAgentConfig to obtain a Tracker.

func (*Client) AgentConfigTemplate

func (c *Client) AgentConfigTemplate(
	key string,
	context ldcontext.Context,
	defaultValue AIAgentConfigDefault,
) AIAgentConfig

AgentConfigTemplate retrieves an AI agent config without performing Mustache interpolation on its instructions. Useful when the caller wants to perform its own interpolation or pass the raw template to an external system. Returns the default value if the config cannot be evaluated.

To send analytic events to LaunchDarkly, call CreateTracker on the returned AIAgentConfig to obtain a Tracker.

func (*Client) AgentConfigs

func (c *Client) AgentConfigs(
	requests []AgentConfigRequest,
	context ldcontext.Context,
) map[string]AIAgentConfig

AgentConfigs retrieves multiple agent configs in batch. Each request is evaluated independently using its own default and variables; failed evaluations use the request's DefaultValue. Emits a single $ld:ai:usage:agent-configs event with the count of requested agents. Does NOT emit per-agent $ld:ai:usage:agent-config events.

func (*Client) AgentGraph

func (c *Client) AgentGraph(
	graphKey string,
	context ldcontext.Context,
	variables map[string]interface{},
) AgentGraphDefinition

AgentGraph retrieves and validates an agent graph for the given graphKey. Always returns a non-nil AgentGraphDefinition. On any validation failure the definition is disabled (Enabled() == false) with an empty node map; traversals are no-ops. CreateTracker is still wired on disabled graphs (when graphKey is non-blank) so callers can record invocation failure.

Pass nil for variables when no interpolation variables are needed.

Emits a single $ld:ai:usage:agent-graph event. Node agent configs are fetched without emitting per-node $ld:ai:usage:agent-config events; node trackers include the graph key.

func (*Client) CompletionConfig

func (c *Client) CompletionConfig(
	key string,
	context ldcontext.Context,
	defaultValue AICompletionConfigDefault,
	variables map[string]interface{},
) AICompletionConfig

CompletionConfig retrieves an AI completion config and interpolates its message templates using the provided variables. Returns the default value if the config cannot be evaluated. Template interpolation is not applied to the default value's messages.

To send analytic events to LaunchDarkly, call CreateTracker on the returned AICompletionConfig to obtain a Tracker.

func (*Client) CompletionConfigTemplate

func (c *Client) CompletionConfigTemplate(
	key string,
	context ldcontext.Context,
	defaultValue AICompletionConfigDefault,
) AICompletionConfig

CompletionConfigTemplate retrieves an AI completion config without performing Mustache interpolation on message content. Useful when the caller wants to perform its own interpolation or pass raw templates to an external system. Returns the default value if the config cannot be evaluated.

To send analytic events to LaunchDarkly, call CreateTracker on the returned AICompletionConfig to obtain a Tracker.

func (*Client) Config deprecated

func (c *Client) Config(
	key string,
	context ldcontext.Context,
	defaultValue AICompletionConfigDefault,
	variables map[string]interface{},
) Config

Config is a deprecated alias for Client.CompletionConfig. Use CompletionConfig instead.

Deprecated: Use Client.CompletionConfig instead.

func (*Client) CreateGraphTracker

func (c *Client) CreateGraphTracker(token string, context ldcontext.Context) (*GraphTracker, error)

CreateGraphTracker reconstructs a GraphTracker from a resumption token and the given context. This delegates to TrackerGraphFromResumptionToken. See that function for details.

func (*Client) CreateTracker

func (c *Client) CreateTracker(token string, context ldcontext.Context) (*Tracker, error)

CreateTracker reconstructs a Tracker from a resumption token and the given context. This delegates to TrackerFromResumptionToken. See that function for details.

func (*Client) JudgeConfig

func (c *Client) JudgeConfig(
	key string,
	context ldcontext.Context,
	defaultValue AIJudgeConfigDefault,
	variables map[string]interface{},
) AIJudgeConfig

JudgeConfig retrieves a Judge AI Config and interpolates its message templates. The reserved variables message_history and response_to_evaluate are preserved as literal placeholders for substitution by Judge.buildMessages during evaluation.

To send analytic events to LaunchDarkly, call CreateTracker on the returned AIJudgeConfig to obtain a Tracker.

func (*Client) JudgeConfigTemplate

func (c *Client) JudgeConfigTemplate(
	key string,
	context ldcontext.Context,
	defaultValue AIJudgeConfigDefault,
) AIJudgeConfig

JudgeConfigTemplate retrieves an AI judge config without performing Mustache interpolation on its messages. Useful when the caller wants to perform its own interpolation or pass raw templates to an external system. Returns the default value if the config cannot be evaluated.

To send analytic events to LaunchDarkly, call CreateTracker on the returned AIJudgeConfig to obtain a Tracker.

type Config deprecated

type Config = AICompletionConfig

Config is an AI Config returned from the LaunchDarkly client.

Deprecated: Use AICompletionConfig, AIAgentConfig, or AIJudgeConfig instead.

type EventSink

type EventSink interface {
	// TrackMetric sends a named analytic event to LaunchDarkly relevant to a particular context, and containing a
	// metric value and additional data.
	TrackMetric(
		eventName string,
		context ldcontext.Context,
		metricValue float64,
		data ldvalue.Value,
	) error
}

EventSink represents the Tracker's requirements for delivering analytic events. This is generally satisfied by the LaunchDarkly SDK's TrackMetric method.

type Feedback

type Feedback string

Feedback represents the feedback provided by a user for a model evaluation.

const (
	// FeedbackPositive is positive feedback.
	FeedbackPositive Feedback = "positive"
	// FeedbackNegative is negative feedback.
	FeedbackNegative Feedback = "negative"
)

type GraphEdge

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

GraphEdge is a directed edge from a source node to a target node in an agent graph. The source is implicit — it is the node that owns this edge.

func (GraphEdge) Handoff

func (e GraphEdge) Handoff() map[string]ldvalue.Value

Handoff returns a defensive copy of optional handoff data associated with this edge, or nil.

func (GraphEdge) Key

func (e GraphEdge) Key() string

Key returns the target node config key.

type GraphMetricSummary

type GraphMetricSummary struct {
	// Success is true after TrackInvocationSuccess, false after TrackInvocationFailure,
	// or unset if neither has been recorded.
	Success ldcommon.Option[bool]
	// DurationMs is the tracked graph-level duration in milliseconds, if recorded.
	DurationMs ldcommon.Option[float64]
	// Tokens is the tracked token usage, if recorded.
	Tokens ldcommon.Option[TokenUsage]
	// Path is the ordered list of node keys visited, or nil if not recorded.
	Path []string
	// ResumptionToken can reconstruct this graph tracker in another process.
	ResumptionToken string
}

GraphMetricSummary is a snapshot of graph-level metrics recorded by a GraphTracker.

type GraphTracker

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

GraphTracker records graph-level metrics for a single agent graph invocation.

Graph-level at-most-once methods (invocation, duration, tokens, path) and GetSummary are safe for concurrent use: the first writer wins and each metric emits at most once. Edge-level methods (redirect, handoff) are multi-fire and do not share that state.

func TrackerGraphFromResumptionToken

func TrackerGraphFromResumptionToken(token string, sdk ServerSDK, context ldcontext.Context) (*GraphTracker, error)

TrackerGraphFromResumptionToken reconstructs a GraphTracker from a token produced by GraphTracker.ResumptionToken, reusing the original runId.

func (*GraphTracker) GetSummary

func (t *GraphTracker) GetSummary() GraphMetricSummary

GetSummary returns a snapshot of graph-level metrics recorded so far.

func (*GraphTracker) ResumptionToken

func (t *GraphTracker) ResumptionToken() string

ResumptionToken returns a URL-safe Base64-encoded token for reconstructing this GraphTracker.

func (*GraphTracker) TrackDuration

func (t *GraphTracker) TrackDuration(durationMs float64) error

TrackDuration records the total wall-clock duration of the graph invocation in milliseconds.

At-most-once. Non-finite values are ignored without consuming the at-most-once slot.

func (*GraphTracker) TrackHandoffFailure

func (t *GraphTracker) TrackHandoffFailure(sourceKey, targetKey string) error

TrackHandoffFailure records a failed handoff from one node to another.

Multi-fire: every call emits an event. Blank keys are ignored.

func (*GraphTracker) TrackHandoffSuccess

func (t *GraphTracker) TrackHandoffSuccess(sourceKey, targetKey string) error

TrackHandoffSuccess records a successful handoff from one node to another.

Multi-fire: every call emits an event. Blank keys are ignored.

func (*GraphTracker) TrackInvocationFailure

func (t *GraphTracker) TrackInvocationFailure() error

TrackInvocationFailure records that the graph invocation failed.

At-most-once and mutually exclusive with TrackInvocationSuccess: whichever is called first wins.

func (*GraphTracker) TrackInvocationSuccess

func (t *GraphTracker) TrackInvocationSuccess() error

TrackInvocationSuccess records that the graph invocation succeeded.

At-most-once and mutually exclusive with TrackInvocationFailure: whichever is called first wins.

func (*GraphTracker) TrackPath

func (t *GraphTracker) TrackPath(path []string) error

TrackPath records the ordered path of node keys visited during the graph invocation.

At-most-once. Nil or empty paths are ignored without consuming the at-most-once slot.

func (*GraphTracker) TrackRedirect

func (t *GraphTracker) TrackRedirect(sourceKey, redirectedTarget string) error

TrackRedirect records a redirect where the graph transitioned to a different target than the edge specified.

Multi-fire: every call emits an event. Blank keys are ignored.

func (*GraphTracker) TrackTotalTokens

func (t *GraphTracker) TrackTotalTokens(tokens TokenUsage) error

TrackTotalTokens records the total token usage for the graph invocation.

At-most-once.

type MetricSummary

type MetricSummary struct {
	// Duration is the tracked duration in milliseconds.
	Duration ldcommon.Option[time.Duration]
	// Feedback is the tracked user feedback (positive or negative).
	Feedback ldcommon.Option[Feedback]
	// Tokens contains information about token usage.
	Tokens ldcommon.Option[TokenUsage]
	// Success indicates whether the operation was successful.
	Success ldcommon.Option[bool]
	// TimeToFirstToken is the time to the first token in milliseconds.
	TimeToFirstToken ldcommon.Option[time.Duration]
	// ToolCalls contains the tool keys recorded via TrackToolCall.
	ToolCalls []string
	// ResumptionToken is the token that can be used to reconstruct this tracker in another process.
	ResumptionToken string
}

MetricSummary represents a summary of metrics tracked by the tracker.

type Metrics

type Metrics struct {
	// Latency is the latency of the request.
	Latency time.Duration
	// TimeToFirstToken is the time to the first token of the streamed response.
	TimeToFirstToken time.Duration
}

Metrics represents the metrics returned by a model provider for a specific request.

type ModelConfig

type ModelConfig struct {
	// Name identifies the model.
	Name string

	// Parameters are model parameters generally set by LaunchDarkly.
	Parameters map[string]ldvalue.Value

	// Custom are custom model parameters generally set by the user.
	Custom map[string]ldvalue.Value
}

ModelConfig holds the model name and associated parameters for an AI Config.

type ProviderConfig

type ProviderConfig struct {
	// Name identifies the provider.
	Name string
}

ProviderConfig holds the provider name for an AI Config.

type ProviderResponse

type ProviderResponse struct {
	// Usage is the token usage.
	Usage TokenUsage
	// Metrics is the request metrics.
	Metrics Metrics
}

ProviderResponse represents the response from a model provider for a specific request.

type ServerSDK

type ServerSDK interface {
	JSONVariation(
		key string,
		context ldcontext.Context,
		defaultVal ldvalue.Value,
	) (ldvalue.Value, error)
	Loggers() interfaces.LDLoggers
	TrackMetric(
		eventName string,
		context ldcontext.Context,
		metricValue float64,
		data ldvalue.Value,
	) error
}

ServerSDK defines the required methods for the AI SDK to interact with LaunchDarkly. These methods are satisfied by the LaunchDarkly Go Server SDK.

type Stopwatch

type Stopwatch interface {
	// Start starts the stopwatch.
	Start()
	// Stop stops the stopwatch and returns the duration since Start was called.
	Stop() time.Duration
}

Stopwatch is used to measure the duration of a task. Start will always be called before Stop. If an implementation is not provided, the Tracker uses a default implementation that delegates to time.Now and time.Since.

type TokenUsage

type TokenUsage struct {
	// Total is the total number of tokens used.
	Total int
	// Input is the number of input tokens used.
	Input int

	// Output is the number of output tokens used.
	Output int
}

TokenUsage represents the token usage returned by a model provider for a specific request.

func (TokenUsage) Set

func (t TokenUsage) Set() bool

Set returns true if any of the fields are non-zero.

type ToolConfig

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

ToolConfig provides read-only access to a tool definition from the root-level tools map. It is distinct from model.parameters.tools which is passed to LLM providers verbatim and must never be modified by the SDK.

func (ToolConfig) CustomParameters

func (t ToolConfig) CustomParameters() map[string]ldvalue.Value

CustomParameters returns a defensive copy of the tool's custom parameters.

func (ToolConfig) Description

func (t ToolConfig) Description() string

Description returns the tool's description.

func (ToolConfig) Name

func (t ToolConfig) Name() string

Name returns the tool's name.

func (ToolConfig) Parameters

func (t ToolConfig) Parameters() map[string]ldvalue.Value

Parameters returns a defensive copy of the tool's parameter definitions.

func (ToolConfig) Type

func (t ToolConfig) Type() string

Type returns the tool's type (e.g., "function").

type TrackData

type TrackData struct {
	// RunID is the unique identifier for this AI run.
	RunID string
	// ConfigKey is the key of the AI Config.
	ConfigKey string
	// Version is the version of the AI Config.
	Version int
	// VariationKey is the variation key of the AI Config. Empty if not set.
	VariationKey string
	// ModelName is the model name associated with the config.
	ModelName string
	// ProviderName is the provider name associated with the config.
	ProviderName string
	// GraphKey is the graph key associated with the config. Empty if not set.
	GraphKey string
	// AISdkName is the name of the AI SDK.
	AISdkName string
	// AISdkVersion is the version of the AI SDK.
	AISdkVersion string
}

TrackData contains the metadata that is attached to every analytic event emitted by a Tracker.

type Tracker

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

Tracker records metrics for a single AI run. Unless otherwise noted, the Tracker's methods are not safe for concurrent use.

All events a Tracker emits share a runId (a UUIDv4) so LaunchDarkly can correlate them in metrics views. See individual track methods for their specific semantics. Call CreateTracker on the AI Config to start a new run. A ResumptionToken preserves the runId, so events emitted by a Tracker reconstructed in another process correlate with the original run.

func TrackerFromResumptionToken

func TrackerFromResumptionToken(token string, sdk ServerSDK, context ldcontext.Context) (*Tracker, error)

TrackerFromResumptionToken reconstructs a Tracker from a token produced by Tracker.ResumptionToken, reusing the original runId.

func (*Tracker) GetSummary

func (t *Tracker) GetSummary() MetricSummary

GetSummary returns a summary of all metrics that have been tracked using this tracker.

func (*Tracker) GetTrackData

func (t *Tracker) GetTrackData() TrackData

GetTrackData returns the metadata that is attached to every analytic event emitted by this Tracker.

func (*Tracker) ResumptionToken

func (t *Tracker) ResumptionToken() string

ResumptionToken returns a URL-safe Base64-encoded token for reconstructing this Tracker.

func (*Tracker) TrackDuration

func (t *Tracker) TrackDuration(dur time.Duration) error

TrackDuration tracks the duration of a task. For example, the duration of a model evaluation request may be tracked here. See also TrackRequest. The duration in milliseconds must fit within a float64.

Records at most once per Tracker; further calls are ignored.

func (*Tracker) TrackDurationOf

func (t *Tracker) TrackDurationOf(operation func() error) error

TrackDurationOf measures the wall-clock duration of operation and tracks it via TrackDuration. Valid for all tracker modes.

func (*Tracker) TrackError

func (t *Tracker) TrackError() error

TrackError tracks an unsuccessful model evaluation.

Records at most once per Tracker. TrackSuccess and TrackError share state; only one of the two can record per Tracker, and subsequent calls are ignored.

func (*Tracker) TrackFeedback

func (t *Tracker) TrackFeedback(feedback Feedback) error

TrackFeedback tracks the feedback provided by a user for a model evaluation. If the feedback is not FeedbackPositive or FeedbackNegative, returns an error and does not track anything.

Records at most once per Tracker; further calls are ignored.

func (*Tracker) TrackJudgeResponse

func (t *Tracker) TrackJudgeResponse(response datamodel.JudgeResponse) error

TrackJudgeResponse tracks the evaluation scores from a judge response.

May be called multiple times per Tracker; each call records the scores from the given response.

func (*Tracker) TrackRequest deprecated

func (t *Tracker) TrackRequest(task func(c *Config) (ProviderResponse, error)) (ProviderResponse, error)

TrackRequest tracks metrics for a model evaluation request. The task function should return a ProviderResponse which can be used to specify request metrics and token usage. All fields of the returned ProviderResponse are optional.

The task function will be passed the current AI Config, which can be used to obtain any parameters or messages relevant to the request.

If the task returns an error, then the request is not considered successful and no metrics are tracked. Otherwise, the following metrics are tracked:

  1. Successful model evaluation.
  2. Any metrics that were that set in the ProviderResponse 2a) If Latency was not set in the ProviderResponse's Metrics field, an automatically measured duration.
  3. Any token usage that was set in the ProviderResponse.

Subsequent calls re-run the task but emit only metrics not already recorded on this Tracker. Call CreateTracker on the AI Config to start a new run.

Deprecated: Use TrackMetricsOf, which is mode-agnostic. TrackRequest is a completion-only convenience and returns an error for agent/judge trackers.

func (*Tracker) TrackSuccess

func (t *Tracker) TrackSuccess() error

TrackSuccess tracks a successful model evaluation.

Records at most once per Tracker. TrackSuccess and TrackError share state; only one of the two can record per Tracker, and subsequent calls are ignored.

func (*Tracker) TrackTimeToFirstToken

func (t *Tracker) TrackTimeToFirstToken(dur time.Duration) error

TrackTimeToFirstToken tracks the time to the first token of the streamed response.

Records at most once per Tracker; further calls are ignored.

func (*Tracker) TrackTokens

func (t *Tracker) TrackTokens(usage TokenUsage) error

TrackTokens tracks the token usage for a model evaluation.

Records at most once per Tracker; further calls are ignored.

func (*Tracker) TrackToolCall

func (t *Tracker) TrackToolCall(toolKey string) error

TrackToolCall tracks a single tool invocation. May be called multiple times per Tracker. The toolKey is included in the event data and recorded in the summary.

func (*Tracker) TrackToolCalls

func (t *Tracker) TrackToolCalls(toolKeys []string) error

TrackToolCalls tracks multiple tool invocations. Calls TrackToolCall for each key.

func (*Tracker) TrackUsage deprecated

func (t *Tracker) TrackUsage(usage TokenUsage) error

TrackUsage tracks token usage.

Deprecated: Use TrackTokens instead.

type TraverseFunc

type TraverseFunc func(node *AgentGraphNode, context map[string]interface{}) interface{}

TraverseFunc visits a node during graph traversal. The return value is available under the node's key in the scoped context of dependency-successor nodes (never written into the caller's initialContext map).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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