Skip to content
VirtualLP Virtual Learning Platform

Prompt Engineering and Calling LLMs from Code

15 min read Topic 25 of 30 2 hands-on labs

Prompt engineering is designing the input so a model reliably produces the output your application needs. In production it is less about clever wording and more about clarity, structure, examples and testing.

Anatomy of a good prompt

System:  You are a customer-support assistant for ShopNow. Answer in JSON only.
User:    Classify the ticket below.

         Categories: billing, delivery, product, other
         Return: {"category": "...", "urgency": "low|medium|high", "summary": "..."}

         <ticket>
         My order ORD-48213 still hasn't arrived and I was charged twice!
         </ticket>
Element Why
Role / system prompt Sets behaviour, tone and rules for the whole conversation
Clear task One explicit instruction (“Classify…”)
Context Data the model needs, separated by delimiters (<ticket>, """)
Constraints Allowed labels, length, language
Output format JSON schema or exact template – makes parsing reliable

Techniques

Technique How Use for
Zero-shot Instruction only Simple, common tasks
Few-shot Add 2–5 input → output examples Specific formats/labels, consistency
Chain-of-thought “Think step by step, then give the answer” Multi-step reasoning, maths
Structured output Ask for JSON, give a schema, validate with code Integration with applications
Prompt chaining Split work into steps (extract → classify → draft) Complex workflows
Grounding (RAG) Put retrieved documents into the prompt, answer only from them Company knowledge, fewer hallucinations

Few-shot example

Review: I love this phone
Sentiment: Positive
Review: Worst service ever
Sentiment: Negative
Review: The battery life is amazing
Sentiment:

Calling LLMs from code – production checklist

  1. Templates – keep prompts in versioned templates with variables (Bedrock Prompt Management or files in Git).
  2. Low temperature (0–0.3) for extraction and classification; parse and validate JSON; retry once on invalid output.
  3. Multi-turn chat – send the conversation history in messages; trim old turns to stay within the context window.
  4. Streaming (ConverseStream) for chat UIs – show tokens as they arrive.
  5. Limits – set maxTokens; handle ThrottlingException with backoff.
  6. Observability – log model ID, latency and usage.inputTokens/outputTokens per call; alarm on cost.
  7. Safety – apply Guardrails; never put secrets in prompts; treat user input as untrusted (prompt injection).
const prompt = (ticket) => `Classify the ticket as billing, delivery, product or other.
Reply only with JSON {"category": "..."}.
<ticket>${ticket}</ticket>`;

Evaluating prompts

Build a small test set of real inputs with expected outputs, run each prompt version against it, and track accuracy, format errors, latency and tokens. Bedrock Model Evaluation can compare models with automatic metrics or human/LLM judges.

Key point

Good prompts are specific, structured and tested: role + task + delimited context + output format, with examples when consistency matters. Measure tokens – they are your bill.

Practise it

Virtual lab · Amazon Web Services · 30 min

Prompt engineering in the Amazon Bedrock playground

Request model access, then practise the core prompt-engineering techniques – clear instructions, system prompts, JSON output, few-shot examples, temperature and max-tokens – while watching tokens, latency and cost for each call.

Open lab

Virtual lab · Amazon Web Services · 30 min

Call an LLM from your code: Bedrock Converse API in Lambda

Build a ticket-triage function that calls a foundation model with the Converse API, fix the IAM permission it needs, return structured JSON and log token usage – the pattern behind most GenAI features in real applications.

Open lab

Quick revision

  1. System prompt sets rules; user prompt carries the task and context.
  2. Use delimiters and an explicit output format.
  3. Few-shot examples teach format and labels.
  4. Low temperature + JSON validation for structured tasks.
  5. Version, evaluate and monitor prompts like code.

Practise this topic

Hands-on virtual labs

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.