llm_anthropic#

class baf.nlp.llm.llm_anthropic.LLMAnthropic(agent, name, parameters, num_previous_messages=1, global_context=None)[source]#

Bases: LLM

LLM wrapper for Anthropic Claude models using the native anthropic SDK.

Anthropic’s API differs enough from the OpenAI shape (system-prompt placement, tool-use schema, required max_tokens, streaming model) that a dedicated implementation is cleaner than relying on the OpenAI-compat endpoint.

Requires nlp.anthropic.api_key to be set in the agent config file (or passed as "api_key" inside parameters for a one-time override).

Parameters:
  • agent (Agent) – the agent the LLM belongs to

  • name (str) – the model identifier (e.g. "claude-opus-4-5", "claude-sonnet-4-5", "claude-haiku-4-5-20251001").

  • parameters (dict) – parameters forwarded to messages.create (e.g. {"temperature": 0.7, "max_tokens": 2048}). May also contain "api_key" which is consumed by initialize() and not forwarded.

  • num_previous_messages (int) – number of previous conversation turns to include in chat() calls (must be > 0).

  • global_context (str) – system-level context injected into every request.

client#

the anthropic.Anthropic client, instantiated in initialize().

num_previous_messages#

number of previous messages used in chat().

Type:

int

_abc_impl = <_abc._abc_data object>#
_build_system(system_message=None, session=None)[source]#

Assemble the Anthropic system parameter.

Anthropic’s API requires a single top-level system string (or NOT_GIVEN), not a list of system-role messages. We concatenate global context, session user context, and the per-call system message in that order.

static _openai_tools_to_anthropic(tools)[source]#

Convert OpenAI-style tool schemas to Anthropic tool format.

OpenAI: {"type": "function", "function": {"name", "description", "parameters"}} Anthropic: {"name", "description", "input_schema"}

_resolve_params(parameters)[source]#

Merge call-time parameters with defaults.

Ensures max_tokens is always present (Anthropic requires it).

chat(session, parameters=None, system_message=None)[source]#

Make a prediction, i.e., generate an output.

This function can provide the chat history to the LLM for the output generation, simulating a conversation or remembering previous messages.

Parameters:
  • session (Session) – the user session

  • parameters (dict) – the LLM parameters. If none is provided, the RAG’s default value will be used

  • system_message (str) – system message to give high priority context to the LLM

Returns:

the LLM output

Return type:

str

initialize()[source]#

Instantiate the Anthropic client.

Reads api_key from self.parameters first (popping it so it is not forwarded to the API call), then falls back to the nlp.anthropic.api_key config-file property.

intent_classification(intent_classifier, message, parameters=None)[source]#

Classify intent via the Anthropic API, returning the same shape as LLMOpenAI.

The output is fed to default_json_to_intent_classifier_predictions() so that intent classifiers remain provider-agnostic.

predict(message, parameters=None, session=None, system_message=None)[source]#

Make a prediction, i.e., generate an output.

Parameters:
  • message (Any) – the LLM input text

  • session (Session) – the ongoing session, can be None if no context needs to be applied

  • parameters (dict) – the LLM parameters to use in the prediction. If none is provided, the default LLM parameters will be used

  • system_message (str) – system message to give high priority context to the LLM

Returns:

the LLM output

Return type:

str

predict_with_tools(messages, tools, parameters=None, system_message=None)[source]#

Make a tool-calling prediction using the native Anthropic SDK.

Input messages and tools use OpenAI schema; this method translates them to the Anthropic format and maps the response back to the shared LLMResponse / ToolCall types so callers remain provider-agnostic.

Parameters:
  • messages (list[dict]) – OpenAI-style chat messages. Tool result messages must include "tool_call_id" matching the call they answer.

  • tools (list[dict]) – OpenAI-style tool schemas.

  • parameters (dict) – extra parameters (e.g. temperature, max_tokens).

  • system_message (str) – high-priority system message.

Returns:

final text or list of tool calls.

Return type:

LLMResponse

set_model(name)[source]#

Set the Claude model identifier.

Parameters:

name (str) – the new model identifier

set_num_previous_messages(num_previous_messages)[source]#

Set the number of previous messages used in chat().

Parameters:

num_previous_messages (int) – the new number of previous messages

baf.nlp.llm.llm_anthropic._DEFAULT_MAX_TOKENS = 4096#

Default max_tokens used when the caller does not supply one.

The Anthropic Messages API requires max_tokens to be provided explicitly; this constant is used as a safe default when it is absent from self.parameters.