Samples
Basic text generation
Generate a simple response:Multi-turn conversation
Continue a conversation with message history:Use a system prompt
Guide Claude’s behavior:Control creativity with temperature
Adjust the randomness of responses:Use tools (function calling)
Enable Claude to call functions:Control stop sequences
Stop generation at specific sequences:Use with API key name
Reference a stored API key:Arguments
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
model | TEXT | - | ✔ | The Claude model to use (e.g., claude-3-5-sonnet-20241022) |
messages | JSONB | - | ✔ | Array of message objects with role and content |
max_tokens | INT | 1024 | ✖ | Maximum tokens to generate (required by Anthropic API) |
api_key | TEXT | NULL | ✖ | Anthropic API key. If not provided, uses configured secret |
api_key_name | TEXT | NULL | ✖ | Name of the secret containing the API key |
base_url | TEXT | NULL | ✖ | Custom API base URL |
timeout | FLOAT8 | NULL | ✖ | Request timeout in seconds |
max_retries | INT | NULL | ✖ | Maximum number of retry attempts |
system_prompt | TEXT | NULL | ✖ | System prompt to guide model behavior |
user_id | TEXT | NULL | ✖ | Unique identifier for the end user |
stop_sequences | TEXT[] | NULL | ✖ | Sequences that stop generation |
temperature | FLOAT8 | NULL | ✖ | Sampling temperature (0.0 to 1.0) |
tool_choice | JSONB | NULL | ✖ | How the model should use tools (e.g., {"type": "auto"}) |
tools | JSONB | NULL | ✖ | Function definitions for tool use |
top_k | INT | NULL | ✖ | Only sample from top K options |
top_p | FLOAT8 | NULL | ✖ | Nucleus sampling threshold (0.0 to 1.0) |
verbose | BOOLEAN | FALSE | ✖ | Enable verbose logging for debugging |
Returns
JSONB: The complete API response including:
id: Unique message identifiertype: Response type (always"message")role: Role of the responder (always"assistant")content: Array of content blocks (text, tool use, etc.)model: Model used for generationstop_reason: Why generation stopped (e.g.,"end_turn","max_tokens")usage: Token usage statistics
Related functions
anthropic_list_models(): list available Claude modelsopenai_chat_complete(): alternative with OpenAI models