DEV Community

Said Olano
Said Olano

Posted on

Prompt Engineering in Spring AI: Crafting Effective AI Instructions

Prompt Engineering in Spring AI: Crafting Effective AI Instructions

Prompt engineering is the art and science of instructing LLMs to produce desired outputs. Well-crafted prompts transform vague capabilities into precise, reliable behavior. Poor prompts lead to inconsistent results; great prompts lead to production-grade intelligence.

Spring AI provides straightforward APIs for building effective prompts.

The Anatomy of a Great Prompt

A production prompt typically includes:

  1. Role/Context - Who or what is the AI?
  2. Task - What should it do?
  3. Constraints - What should it NOT do?
  4. Format - How should output be structured?
  5. Examples - Show, don't just tell

Basic Prompt Structure in Spring AI

@Service
public class PromptPatterns {
    private final ChatClient chatClient;

    public String systemPromptExample(String userQuery) {
        String system = "You are a helpful technical support specialist. " +
            "Provide clear, concise answers. Use examples when helpful.";

        return this.chatClient
            .prompt()
            .system(system)
            .user(userQuery)
            .call()
            .content();
    }
}
Enter fullscreen mode Exit fullscreen mode

Prompt Pattern #1: Role-Based Prompting

Give the AI a specific role to inhabit:

public String technicalExplainer(String topic) {
    String prompt = "You are a senior software architect. " +
        "Explain to a junior developer using analogies and examples.\n\n" +
        "Topic: " + topic;

    return chatClient.prompt().user(prompt).call().content();
}
Enter fullscreen mode Exit fullscreen mode

Prompt Pattern #2: Few-Shot Prompting

Show examples of desired behavior:

public String classifySentiment(String text) {
    String prompt = "Classify sentiment as POSITIVE, NEGATIVE, or NEUTRAL.\n\n" +
        "Examples:\n" +
        "'I love this!' → POSITIVE\n" +
        "'This is terrible.' → NEGATIVE\n" +
        "'The sky is blue.' → NEUTRAL\n\n" +
        "Text: \"" + text + "\"\n" +
        "Classification:";

    return chatClient.prompt().user(prompt).call().content();
}
Enter fullscreen mode Exit fullscreen mode

Prompt Pattern #3: Chain-of-Thought Prompting

Encourage step-by-step reasoning:

public String problemSolving(String problem) {
    String prompt = "Solve step by step: (1) What do we know? " +
        "(2) Break it down. (3) Work through it. (4) Verify.\n\n" +
        "Problem: " + problem;

    return chatClient.prompt().user(prompt).call().content();
}
Enter fullscreen mode Exit fullscreen mode

Prompt Pattern #4: Constraint-Based Prompting

Set clear boundaries:

public String guardrailedResponse(String query) {
    String prompt = "Answer following these rules:\n" +
        "1. ONLY use provided context\n" +
        "2. Do NOT make up information\n" +
        "3. Say 'I don't know' if uncertain\n" +
        "4. Keep answer under 200 words\n\n" +
        "Query: " + query;

    return chatClient.prompt().user(prompt).call().content();
}
Enter fullscreen mode Exit fullscreen mode

Temperature Control

Match temperature to use case:

public String deterministicResponse(String query) {
    // Low temperature = consistent, predictable
    return chatClient.prompt().user(query)
        .options(opts -> opts.withTemperature(0.0))
        .call().content();
}

public String creativeResponse(String query) {
    // High temperature = creative, variable
    return chatClient.prompt().user(query)
        .options(opts -> opts.withTemperature(1.0))
        .call().content();
}
Enter fullscreen mode Exit fullscreen mode

Anti-Patterns to Avoid

❌ Too vague: "Tell me about Java"
✅ Specific: "Explain Spring Framework's dependency injection for a 2-year Java developer with code examples."

❌ No format: "List best practices"
✅ Formatted: "List 5 practices as [Number]. [Practice]: [1-2 sentence explanation]"

Best Practices

  1. Be Specific - Vague prompts lead to vague results
  2. Show Examples - Few-shot prompting works better than instruction
  3. Set Boundaries - Explicitly state what NOT to do
  4. Specify Format - Tell the model how to structure output
  5. Use Roles - Role-based prompting improves accuracy
  6. Test and Iterate - Prompt engineering is empirical
  7. Version Prompts - Treat prompts like code

Conclusion

Prompt engineering is both art and science. Start with clear, specific instructions, use examples, bound outputs, and test different approaches.

In Spring AI, mastering prompt engineering transforms ordinary interactions into reliable, production-grade intelligence.

Top comments (0)