Documentation
¶
Index ¶
- Variables
- type Budget
- type Convo
- func (c *Convo) CancelToolUse(toolUseID string, err error) error
- func (c *Convo) CumulativeUsage() CumulativeUsage
- func (c *Convo) DebugJSON() ([]byte, error)
- func (c *Convo) Depth() int
- func (c *Convo) GetID() string
- func (c *Convo) LastUsage() llm.Usage
- func (c *Convo) OverBudget() error
- func (c *Convo) ResetBudget(budget Budget)
- func (c *Convo) SendMessage(msg llm.Message) (*llm.Response, error)
- func (c *Convo) SendUserTextMessage(s string, otherContents ...llm.Content) (*llm.Response, error)
- func (c *Convo) SubConvo() *Convo
- func (c *Convo) SubConvoWithHistory() *Convo
- func (c *Convo) ToolResultCancelContents(resp *llm.Response) ([]llm.Content, error)
- func (c *Convo) ToolResultContents(ctx context.Context, resp *llm.Response) ([]llm.Content, bool, error)
- type CumulativeUsage
- type Listener
- type NoopListener
- func (n *NoopListener) OnRequest(ctx context.Context, convo *Convo, id string, msg *llm.Message)
- func (n *NoopListener) OnResponse(ctx context.Context, convo *Convo, id string, msg *llm.Response)
- func (n *NoopListener) OnToolCall(ctx context.Context, convo *Convo, id string, toolName string, ...)
- func (n *NoopListener) OnToolResult(ctx context.Context, convo *Convo, id string, toolName string, ...)
- type ToolCallInfo
Constants ¶
This section is empty.
Variables ¶
var ErrDoNotRespond = errors.New("do not respond")
Functions ¶
This section is empty.
Types ¶
type Budget ¶
type Budget struct {
MaxDollars float64 // if > 0, max dollars that may be spent
}
A Budget represents the maximum amount of resources that may be spent on a conversation. Note that the default (zero) budget is unlimited.
type Convo ¶
type Convo struct {
// ID is a unique ID for the conversation
ID string
// Ctx is the context for the entire conversation.
Ctx context.Context
// Service is the LLM service to use.
Service llm.Service
// Tools are the tools available during the conversation.
Tools []*llm.Tool
// SystemPrompt is the system prompt for the conversation.
SystemPrompt string
// PromptCaching indicates whether to use Anthropic's prompt caching.
// See https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching#continuing-a-multi-turn-conversation
// for the documentation. At request send time, we set the cache_control field on the
// last message. We also cache the system prompt.
// Default: true.
PromptCaching bool
// ToolUseOnly indicates whether Claude may only use tools during this conversation.
// TODO: add more fine-grained control over tool use?
ToolUseOnly bool
// Parent is the parent conversation, if any.
// It is non-nil for "subagent" calls.
// It is set automatically when calling SubConvo,
// and usually should not be set manually.
Parent *Convo
// Budget is the budget for this conversation (and all sub-conversations).
// The Conversation DOES NOT automatically enforce the budget.
// It is up to the caller to call OverBudget() as appropriate.
Budget Budget
// Hidden indicates that the output of this conversation should be hidden in the UI.
// This is useful for subconversations that can generate noisy, uninteresting output.
Hidden bool
// ExtraData is extra data to make available to all tool calls.
ExtraData map[string]any
// Listener receives messages being sent.
Listener Listener
// contains filtered or unexported fields
}
A Convo is a managed conversation with Claude. It automatically manages the state of the conversation, including appending messages send/received, calling tools and sending their results, tracking usage, etc.
Exported fields must not be altered concurrently with calling any method on Convo. Typical usage is to configure a Convo once before using it.
func New ¶
New creates a new conversation with Claude with sensible defaults. ctx is the context for the entire conversation.
func (*Convo) CumulativeUsage ¶
func (c *Convo) CumulativeUsage() CumulativeUsage
func (*Convo) DebugJSON ¶ added in v0.0.12
DebugJSON returns the conversation history as JSON for debugging purposes.
func (*Convo) Depth ¶
Depth reports how many "sub-conversations" deep this conversation is. That it, it walks up parents until it finds a root.
func (*Convo) OverBudget ¶
OverBudget returns an error if the convo (or any of its parents) has exceeded its budget. TODO: document parent vs sub budgets, multiple errors, etc, once we know the desired behavior.
func (*Convo) ResetBudget ¶
ResetBudget sets the budget to the passed in budget and adjusts it by what's been used so far.
func (*Convo) SendMessage ¶
SendMessage sends a message to Claude. The conversation records (internally) all messages succesfully sent and received.
func (*Convo) SendUserTextMessage ¶
SendUserTextMessage sends a text message to the LLM in this conversation. otherContents contains additional contents to send with the message, usually tool results.
func (*Convo) SubConvo ¶
SubConvo creates a sub-conversation with the same configuration as the parent conversation. (This propagates context for cancellation, HTTP client, API key, etc.) The sub-conversation shares no messages with the parent conversation. It does not inherit tools from the parent conversation.
func (*Convo) SubConvoWithHistory ¶
func (*Convo) ToolResultCancelContents ¶
func (*Convo) ToolResultContents ¶
func (c *Convo) ToolResultContents(ctx context.Context, resp *llm.Response) ([]llm.Content, bool, error)
ToolResultContents runs all tool uses requested by the response and returns their results. Cancelling ctx will cancel any running tool calls. The boolean return value indicates whether any of the executed tools should end the turn.
type CumulativeUsage ¶
type CumulativeUsage struct {
StartTime time.Time `json:"start_time"`
Responses uint64 `json:"messages"` // count of responses
InputTokens uint64 `json:"input_tokens"`
OutputTokens uint64 `json:"output_tokens"`
CacheReadInputTokens uint64 `json:"cache_read_input_tokens"`
CacheCreationInputTokens uint64 `json:"cache_creation_input_tokens"`
TotalCostUSD float64 `json:"total_cost_usd"`
ToolUses map[string]int `json:"tool_uses"` // tool name -> number of uses
}
CumulativeUsage represents cumulative usage across a Convo, including all sub-conversations.
func (*CumulativeUsage) Add ¶
func (u *CumulativeUsage) Add(usage llm.Usage)
func (CumulativeUsage) Attr ¶
func (u CumulativeUsage) Attr() slog.Attr
Attr returns the cumulative usage as a slog.Attr with key "usage".
func (*CumulativeUsage) Clone ¶
func (u *CumulativeUsage) Clone() CumulativeUsage
func (*CumulativeUsage) DollarsPerHour ¶
func (u *CumulativeUsage) DollarsPerHour() float64
func (*CumulativeUsage) TotalInputTokens ¶
func (u *CumulativeUsage) TotalInputTokens() uint64
TotalInputTokens returns the grand total cumulative input tokens in u.
func (*CumulativeUsage) WallTime ¶
func (u *CumulativeUsage) WallTime() time.Duration
type Listener ¶
type Listener interface {
// TODO: Content is leaking an anthropic API; should we avoid it?
// TODO: Where should we include start/end time and usage?
OnToolCall(ctx context.Context, convo *Convo, toolCallID string, toolName string, toolInput json.RawMessage, content llm.Content)
OnToolResult(ctx context.Context, convo *Convo, toolCallID string, toolName string, toolInput json.RawMessage, content llm.Content, result *string, err error)
OnRequest(ctx context.Context, convo *Convo, requestID string, msg *llm.Message)
OnResponse(ctx context.Context, convo *Convo, requestID string, msg *llm.Response)
}
type NoopListener ¶
type NoopListener struct{}
func (*NoopListener) OnResponse ¶
func (*NoopListener) OnToolCall ¶
func (n *NoopListener) OnToolCall(ctx context.Context, convo *Convo, id string, toolName string, toolInput json.RawMessage, content llm.Content)
func (*NoopListener) OnToolResult ¶
type ToolCallInfo ¶
func ToolCallInfoFromContext ¶
func ToolCallInfoFromContext(ctx context.Context) ToolCallInfo