message
Overview
Name | message |
Type | Resource |
Id | anthropic.messages.message |
Fields
Name | Datatype | Description |
---|---|---|
id | string | Unique object identifier. |
content | array | The generated content blocks by the model. |
model | string | The model that handled the request. |
role | string | The conversational role of the generated message. |
stop_reason | string | The reason the model stopped generating. |
stop_sequence | string | The stop sequence that caused the model to stop, if applicable. |
type | string | Object type, which is always "message" for Messages API. |
usage | object | Information about token usage and rate limits. |
Methods
Name | Accessible by | Required Params |
---|---|---|
create_message | SELECT | anthropic-version, data__max_tokens, data__messages, data__model |
Required Parameters for Message Creation
Parameter | Description |
---|---|
anthropic-version | API version string (e.g., '2023-06-01') |
data__max_tokens | Maximum number of tokens to generate |
data__messages | Array of message objects with role and content |
data__model | Model identifier (e.g., 'claude-3-5-sonnet-20240620') |
Model Comparison Table
To help you choose the right model for your needs, we’ve compiled a table comparing the key features and capabilities of each model in the Claude family:
Model | Claude 3.5 Sonnet | Claude 3 Opus | Claude 3 Sonnet | Claude 3 Haiku |
---|---|---|---|---|
Description | Most intelligent model | Powerful model for highly complex tasks | Balance of intelligence and speed | Fastest and most compact model for near-instant responsiveness |
Strengths | Highest level of intelligence and capability | Top-level performance, intelligence, fluency, and understanding | Strong utility, balanced for scaled deployments | Quick and accurate targeted performance |
Multilingual | Yes | Yes | Yes | Yes |
Vision | Yes | Yes | Yes | Yes |
Message Batches API | Yes | Yes | No | Yes |
API model name | Upgraded version: claude-3-5-sonnet-20241022 Previous version: claude-3-5-sonnet-20240620 | claude-3-opus-20240229 | claude-3-sonnet-20240229 | claude-3-haiku-20240307 |
Comparative latency | Fast | Moderately fast | Fast | Fastest |
Context window | 200K | 200K | 200K | 200K |
Max output | 8192 tokens | 4096 tokens | 4096 tokens | 4096 tokens |
Cost (Input / Output per MTok) | $3.00 / $15.00 | $15.00 / $75.00 | $3.00 / $15.00 | $0.25 / $1.25 |
Training data cut-off | Apr 2024 | Aug 2023 | Aug 2023 | Aug 2023 |
Query Examples
Basic Message Generation
This example generates a one-sentence summary of StackQL:
SELECT
model as model,
role as role,
stop_reason as stop_reason,
stop_sequence as stop_sequence,
JSON_EXTRACT(usage, '$.input_tokens') as input_tokens,
JSON_EXTRACT(usage, '$.output_tokens') as output_tokens,
JSON_EXTRACT(json_each.value, '$.text') as content
FROM
anthropic.messages.message,
JSON_EACH(content)
WHERE
"anthropic-version" = '2023-06-01'
AND data__model = 'claude-3-5-sonnet-20240620'
AND data__max_tokens = 1024
AND data__messages = '[{"role": "user", "content": "one sentence summary of stackql"}]'
Example response:
|----------------------------|-----------|-------------|---------------|--------------|---------------|--------------------------------|
| model | role | stop_reason | stop_sequence | input_tokens | output_tokens | content |
|----------------------------|-----------|-------------|---------------|--------------|---------------|--------------------------------|
| claude-3-5-sonnet-20240620 | assistant | end_turn | null | 13 | 46 | StackQL is a SQL-like query |
| | | | | | | language and runtime for |
| | | | | | | cloud infrastructure, allowing |
| | | | | | | users to query, analyze, and |
| | | | | | | manage cloud resources across |
| | | | | | | multiple providers using |
| | | | | | | familiar SQL syntax. |
|----------------------------|-----------|-------------|---------------|--------------|---------------|--------------------------------|
Multi-turn Conversation Example
This example shows how to conduct a multi-turn conversation:
SELECT
JSON_EXTRACT(json_each.value, '$.text') as content
FROM
anthropic.messages.message,
JSON_EACH(content)
WHERE
"anthropic-version" = '2023-06-01'
AND data__model = 'claude-3-5-sonnet-20240620'
AND data__max_tokens = 1024
AND data__messages = '[
{"role": "user", "content": "What is cloud infrastructure?"},
{"role": "assistant", "content": "Cloud infrastructure refers to the hardware and software components needed for cloud computing, including servers, storage, and networking resources."},
{"role": "user", "content": "How can StackQL help manage it?"}
]'
Using System Prompts
Example showing how to include a system prompt:
SELECT
JSON_EXTRACT(json_each.value, '$.text') as content
FROM
anthropic.messages.message,
JSON_EACH(content)
WHERE
"anthropic-version" = '2023-06-01'
AND data__model = 'claude-3-5-sonnet-20240620'
AND data__max_tokens = 1024
AND data__system = 'You are a cloud infrastructure expert focusing on technical details.'
AND data__messages = '[{"role": "user", "content": "Explain how StackQL helps with cloud resource management"}]'
Usage Notes
Token Usage
- The
usage
object contains information about token consumption:input_tokens
: Number of tokens in the inputoutput_tokens
: Number of tokens generated in the response
- Token usage affects billing and rate limits
- Token counts may not exactly match visible content due to internal processing
Response Processing
- Use
JSON_EACH
to process the content array - Content is returned as blocks with type and text
- Use
JSON_EXTRACT
to access nested JSON fields like usage statistics
Best Practices
- Always specify an appropriate
max_tokens
value - Include the correct
anthropic-version
header - Format message arrays properly as JSON strings
- Use system prompts for specialized behavior
- Monitor token usage for cost optimization
Error Handling
Common error scenarios to handle:
- Invalid model specification
- Malformed message JSON
- Token limit exceeded
- Rate limiting
- Authentication failures
Rate Limits
- Token-based rate limiting
- Monitored through the usage object
- Consider implementing backoff strategies for heavy usage
Version Compatibility
- Specify API version using
anthropic-version
- Current supported version: '2023-06-01'
- Check Anthropic's documentation for version updates
Advanced Usage Examples
Temperature Control
Adjust response randomness with the temperature parameter:
/* Lower temperature for more focused responses */
SELECT
JSON_EXTRACT(json_each.value, '$.text') as content
FROM
anthropic.messages.message,
JSON_EACH(content)
WHERE
"anthropic-version" = '2023-06-01'
AND data__model = 'claude-3-5-sonnet-20240620'
AND data__max_tokens = 1024
AND data__temperature = 1
AND data__messages = '[{"role": "user", "content": "List 3 key benefits of using StackQL"}]'
Using Stop Sequences
Configure custom stop sequences to control response length:
SELECT
JSON_EXTRACT(json_each.value, '$.text') as content,
stop_reason,
stop_sequence
FROM
anthropic.messages.message,
JSON_EACH(content)
WHERE
"anthropic-version" = '2023-06-01'
AND data__model = 'claude-3-5-sonnet-20240620'
AND data__max_tokens = 1024
AND data__stop_sequences = '["END", "STOP"]'
AND data__messages = '[{"role": "user", "content": "Describe StackQL until you reach a natural conclusion END"}]'
Metadata Tracking
Include metadata for request tracking:
SELECT
JSON_EXTRACT(json_each.value, '$.text') as content,
JSON_EXTRACT(usage, '$.input_tokens') as input_tokens,
JSON_EXTRACT(usage, '$.output_tokens') as output_tokens
FROM
anthropic.messages.message,
JSON_EACH(content)
WHERE
"anthropic-version" = '2023-06-01'
AND data__model = 'claude-3-5-sonnet-20240620'
AND data__max_tokens = 1024
AND data__metadata = '{"user_id": "u123"}'
AND data__messages = '[{"role": "user", "content": "What is StackQL?"}]'
Working with Response Data
Content Block Structure
The content field returns an array of blocks with this structure:
{
"type": "text",
"text": "The actual content..."
}
Access specific parts using JSON functions:
-- Get just the text content
JSON_EXTRACT(json_each.value, '$.text')
-- Get the content type
JSON_EXTRACT(json_each.value, '$.type')
Usage Statistics Processing
Extract detailed usage information:
SELECT
JSON_EXTRACT(usage, '$.input_tokens') as input_tokens,
JSON_EXTRACT(usage, '$.output_tokens') as output_tokens,
JSON_EXTRACT(usage, '$.cache_creation_input_tokens') as cache_creation_tokens,
JSON_EXTRACT(usage, '$.cache_read_input_tokens') as cache_read_tokens
FROM
anthropic.messages.message
WHERE
-- query conditions
Performance Optimization
Token Usage Optimization
Tips for minimizing token usage:
- Use precise prompts
- Set appropriate max_tokens
- Use system prompts effectively
- Leverage stop sequences
- Monitor and analyze usage patterns
Response Processing Optimization
Strategies for efficient response handling:
- Use appropriate JSON extraction
- Implement efficient error handling
- Process responses asynchronously when possible
- Cache frequently used responses
- Implement retry logic with exponential backoff