Prompting 101 for SWEs - Role, Context & Goals (RCG)
Let’s start with some core principles for building an effective prompt;
Role, Context & Goals (RCG)
Role
Put the LLM into the shoes of the person you’d like it to be.
❌ | “rewrite this function …” |
✅ | “You are a senior software engineer and an expert in python. Rewrite the following function to ...” |
Context
LLMs only know what you tell them. So, make sure to give it all the necessary details!
❌ | “why does this not work…” |
✅ | “I have a python rest api built using flask, one of the routing functions is throwing a type error. Here’s the error ...” |
Goal
Garbage in = garbage out, vague prompts result in useless answers. Being specific about your end goals helps guide the LLM to produce something usable.
❌ | “make this work…” |
✅ | “this python function is returning None rather than the <expected result>. Given the code below can you identify and fix the problem.” |
Examples
If possible, show what you want using an example.
✅ | “When the function receives [9,5,6] it should return [5,6,9]. Given the code below can you identify and fix the problem.” |
✅ Prompt Cheat Sheet
Type | Prompt Template | Use case |
---|---|---|
Role Context Goal | You are a {role} and an expert in {language}. This {program} written in {language} using {framework/tool} is used for {context}. Review this {function/file} for {goal} | Code reviews, debugging, refactoring |
Triage | You are a senior software engineer and an expert in {language}. This {program} written in {language} using {framework/tool} is used for {context}. Walk through {function/file} line by line. [For the given {input}]. What are the variable values? Where might it break? | Debugging, simulating runtime behavior, potentially surface hidden bugs. |
Creation with chaining | You are a senior software engineer and an expert in {language}. I’m building a {goal} with the following requirements: {context}, using {language} and {framework/tool}. First generate a skeleton of the components. Next we’ll add state. Then handle API calls… | Feature development, chaining |
❌ Prompt Anti-Pattern
Type | Prompt | Notes |
---|---|---|
Vague | Write a function to do X Please fix the function below… |
For an LLM to provide relevant responses, specific details must be included. |
Overloaded | Generate a complete web application with user authentication, a front end using angular with tests and deployment scripts | Do one thing at a time, create a feedback loop where you as a human can verify if we’re still headed in the right direction |
No question | Here's my code … | Always include a clear ask |
Success Criteria | Improve my code … | Vague success criteria will result in suboptimal responses. |