Skip to main content

Prompt Engineering

Effective prompts are crucial for getting high-quality results from language models. This guide covers best practices and tips for prompt engineering with AI SDK Core.

Basic Principles

Be Specific and Clear

Provide clear, specific instructions:

Use System Messages

Set the model’s role and behavior with system messages:

Provide Context

Include relevant context in your prompts:

Prompts for Tools

When using tools, follow these best practices:

1. Use Strong Models

Use models that excel at tool calling:

2. Limit the Number of Tools

Keep tools focused and limited (5 or fewer recommended):

3. Use Descriptive Names

Choose semantic, meaningful names:

4. Write Detailed Descriptions

Provide comprehensive tool and parameter descriptions:

5. Keep Schemas Simple

Avoid overly complex nested structures:

6. Document Tool Output

When tool dependencies exist, describe output in the tool description:

7. Provide Examples

Include example tool usage in prompts:

Prompts for Structured Data

Use Clear Property Descriptions

Handle Dates Properly

Use string schemas with transformations for dates:

Use Nullable for Optional Fields

For maximum compatibility with strict schemas:

Set Temperature to 0

For deterministic structured output:

Debugging Prompts

Inspect Warnings

Check for unsupported features:

View Request Bodies

Inspect the actual request sent to the provider:

Log Tool Calls

Monitor tool execution:

Common Patterns

Few-Shot Prompting

Provide examples in your prompt:

Chain of Thought

Ask the model to explain its reasoning:

Role-Based Prompting

Assign the model a specific role:

Constrained Generation

Use clear constraints:

Tips Summary

General Prompts

  1. Be specific and clear
  2. Provide context and examples
  3. Use system messages to set behavior
  4. Add constraints when needed
  5. Request step-by-step reasoning for complex tasks

Tool Prompts

  1. Use strong models (GPT-5, Claude 4 Sonnet)
  2. Limit to 5 or fewer tools
  3. Use descriptive names and detailed descriptions
  4. Add .describe() to all schema properties
  5. Keep schemas simple and flat
  6. Document tool output formats
  7. Include example tool usage

Structured Data

  1. Add property descriptions
  2. Use z.string().date() for dates, then transform
  3. Use .nullable() instead of .optional()
  4. Set temperature: 0 for consistency
  5. Provide output name and description

Debugging

  1. Check result.warnings for unsupported features
  2. Inspect result.request.body to see actual requests
  3. Use callbacks to log tool calls and results
  4. Test with different models to compare behavior

Next Steps

Generating Text

Learn about text generation functions

Tool Calling

Master tool calling techniques

Structured Data

Generate type-safe structured data

Settings

Configure model parameters