Class: Chorus::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chorus/client.rb

Overview

Thin wrapper around the Anthropic Messages API. This is the ONLY class in Chorus allowed to perform HTTP calls — every agent goes through it.

Constant Summary collapse

API_URL =
"https://api.anthropic.com/v1/messages"
ANTHROPIC_VERSION =
"2023-06-01"
DEFAULT_MODEL =
"claude-opus-4-8"
DEFAULT_MAX_TOKENS =
4096

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV.fetch("ANTHROPIC_API_KEY", nil), model: DEFAULT_MODEL) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String, nil) (defaults to: ENV.fetch("ANTHROPIC_API_KEY", nil))

    Anthropic API key. Defaults to ENV.

  • model (String) (defaults to: DEFAULT_MODEL)

    model id to use for every call made by this client.

Raises:



28
29
30
31
32
33
# File 'lib/chorus/client.rb', line 28

def initialize(api_key: ENV.fetch("ANTHROPIC_API_KEY", nil), model: DEFAULT_MODEL)
  raise MissingAPIKeyError if api_key.nil? || api_key.empty?

  @api_key = api_key
  @model = model
end

Instance Method Details

#chat(system_prompt:, messages:, max_tokens: DEFAULT_MAX_TOKENS) ⇒ String

Sends a single-turn (or multi-turn) request to the Messages API.

Parameters:

  • system_prompt (String)

    the system prompt describing the agent's role.

  • messages (Array<Hash>)

    conversation turns, each {role:, content:}.

  • max_tokens (Integer) (defaults to: DEFAULT_MAX_TOKENS)

    maximum tokens to generate.

Returns:

  • (String)

    the text of Claude's response.



41
42
43
44
# File 'lib/chorus/client.rb', line 41

def chat(system_prompt:, messages:, max_tokens: DEFAULT_MAX_TOKENS)
  response = post_message(system_prompt: system_prompt, messages: messages, max_tokens: max_tokens)
  extract_text(response)
end