Quick answer: The generative AI best practices that matter most for freshers are: define the task first, write specific prompts, give the model trusted facts, validate its output, test with a fixed set of questions, keep prompts and API code organised, protect keys and private data, and watch cost. Start with the simplest design that works and add complexity only when a test shows you need it.
These generative AI best practices help you go from "it worked once on my laptop" to "it works, and I can show why". You do not need a huge project to use them. Even a one-page document helper becomes better when its prompt is clear, its output is checked and its keys are safe.
This guide follows the order of a real build: plan, prompt, code, test, protect and improve. The examples use simple Python, so you can use them with any AI service.
Generative AI best practices at a glance
| Stage | Do this | Avoid this |
|---|---|---|
| Plan | Define one user and one task | "An AI that does everything" |
| Prompt | Give rules, facts and a format | One vague sentence |
| Code | Keep prompts and API calls in small functions | API code copied into every file |
| Test | Run a fixed list of questions | Try one favourite question |
| Safety | Limit data and actions | Send private data without a reason |
| Cost | Count tokens and set limits | Use the largest model for every task |
Plan before you call a model
Decide whether you need AI at all
If a normal rule gives the correct answer, use the rule. Adding GST to a price, checking whether a date has passed or finding a roll number does not need a language model. Normal code is faster, cheaper and gives the same result each time.
Generative AI suits tasks with messy language: summarising feedback, drafting a reply or pulling facts from text written in many styles.
Write the finish line first
Say what success means in one line. For example: "Given a customer email, return the correct topic from four choices, and return unknown if none fits."
Then collect ten sample emails and their correct topics. You now have a first test set before you have any code.
Pro tip: Start with one narrow task. A helper that answers questions about one library's rules can be tested well. "A chatbot for college students" cannot, because nobody knows where the job ends.
Generative AI tips for better prompts
A useful prompt has five parts:
- Role: who the answer is for.
- Task: one clear action.
- Data: the text to use, marked clearly.
- Rules: limits and what to do if facts are missing.
- Format: the exact shape of the answer.
Here is a simple template:
You help a college library answer student questions.
Task: Answer the question using only the rules below.
If the rules do not contain the answer, say "I do not know".
Use at most 2 sentences.
<rules>
Books may be kept for 14 days. The fine is Rs 2 per day.
</rules>
<question>
How long can I keep a book?
</question>
The tags show which part is data. They make the prompt easier to read, but they do not fully stop prompt injection. A user can still type a harmful instruction, so you need checks in code too.
Other habits that help:
- Use positive instructions. "Write two sentences" is clearer than "Do not make it long".
- Show one or two examples when the format matters.
- Split big jobs. First extract facts, then write the summary.
- Allow "I do not know". If you never allow it, the model may guess.
- Change one thing at a time, then test again.
Generative AI clean code: keep the parts separate
In a first project, it is common to see the API key, prompt, model call, parsing and screen code in one long function. It is hard to test, and one change can break everything.
Split the work into small parts.
Put the prompt in a function
def build_prompt(rules, question):
return (
"Answer using only the rules.\n"
'If the answer is missing, say "I do not know".\n'
f"<rules>{rules}</rules>\n"
f"<question>{question}</question>"
)
You can print this prompt and read it without calling the AI service. Keep it in Git, so you can see which wording gave which result.
Pass the model call into your code
def answer(rules, question, ask_model):
if not question.strip():
return "Please type a question."
reply = ask_model(build_prompt(rules, question))
return reply.strip()
def fake_model(prompt):
return " You can keep a book for 14 days. "
print(answer("Books: 14 days", "How long?", fake_model))
print(answer("Books: 14 days", " ", fake_model))
Output:
You can keep a book for 14 days.
Please type a question.
This uses build_prompt from the block above. Notice that answer does not know which company made the model. In a test you pass fake_model. In the real app you pass a function that calls your AI service. Tests are then quick and free, and changing provider means changing one function.
Keep settings in one place
from dataclasses import dataclass
@dataclass(frozen=True)
class Settings:
max_question_chars: int = 500
max_tries: int = 3
timeout_seconds: int = 20
settings = Settings()
print(settings.timeout_seconds)
This prints 20. frozen=True stops code from changing a setting by accident. Your model name can live here too, but never the API key. Read that from an environment variable.
Common mistake: Adding a large framework on the first day. If the app makes one model call, a few plain functions are easier to read and fix. Add a framework when you can name the problem it solves.
Check the output and test every change
Validate first
Model output is like a form filled in by a stranger. Check it before your app uses it.
ALLOWED = {"fees", "timing", "syllabus", "unknown"}
def clean_topic(reply):
topic = reply.strip().lower()
return topic if topic in ALLOWED else "unknown"
print(clean_topic(" Fees\n"))
print(clean_topic("The topic is fees"))
Output:
fees
unknown
The second reply contains a useful word, but it does not follow the format. Returning unknown is safer than guessing. Count these cases: if there are many, improve the prompt.
Keep a small test set
Include normal questions, spelling mistakes, very long input, empty input, questions in mixed Telugu and English, and questions your data cannot answer. Save the expected result for each.
For a RAG app, score two things separately:
- Retrieval: Did the search find the right passage?
- Answer: Did the reply use that passage correctly?
This tells you which half to fix. If you use another AI model as a judge, check some of its scores by hand.
Interview note: "How did you know your prompt improved?" is a common project question. "It looked better" is weak. "It passed 24 of 30 tests before and 28 after, and here are the two that still fail" is strong, provided those are your real numbers.
Generative AI guidelines for safety and privacy
Follow these generative AI guidelines even in a student project:
- Keep keys out of code. Use environment variables and add
.envto.gitignore. - Send less personal data. Remove names and phone numbers if the task does not need them.
- Read the service's data rules before sending real information.
- Check access in code. Do not let a prompt decide who may see a file.
- Limit tools. An agent that only needs to read a calendar should not be able to delete events.
- Ask a person first for payments, emails, deletions and other risky actions.
- Tell users they are reading AI output and show sources where you can.
- Check for unfair results. Try the same CV or request with different names.
The same care applies when you use AI to write code. Read every line, run it and check that each package it suggests is real and well kept before you install it.
Control cost and speed
You usually pay for the tokens you send and receive. Four habits keep that low:
- Remove text that does not help answer the question.
- Set a limit on answer length.
- Save answers to repeated public questions, but not one user's private answer for another user.
- Try a smaller model first, then move up if your tests show it is not good enough.
Record the time, token count and result of each call. Do not save private prompts in logs unless you have a clear reason, permission and a deletion plan.
A fresher's project checklist
Before you share your app, can you say yes to these?
- The README explains the user, task and limits in plain words.
- The app handles empty, long and unrelated input.
- Output is checked before use.
- At least 20 test questions are in the repository.
- Retries and timeouts are in place.
- No key or private data is in Git history.
- I can explain each part without notes.
To apply this, choose one of our generative AI projects and compare it with the bugs in common generative AI mistakes. For ideas from working life, see generative AI real time examples. Our guide to writing a good project README helps you present it.
New to the subject? The AI engineer roadmap gives you the learning order. The artificial intelligence course guide explains what a full syllabus should include, and the CodeBegun Artificial Intelligence course can be joined in our Madhapur, Hyderabad classroom or live online from any city in Telangana and Andhra Pradesh.
Frequently asked questions
What is the first generative AI best practice a fresher should learn?
Write down what a good answer looks like before you write the prompt. Pick ten sample inputs and the result you expect for each. This gives you a clear goal and a way to test every change.
Do I need LangChain to follow best practices?
No. A small app can call an AI service with plain Python functions. Learn the basic flow first, then use a framework if it saves you real work. You should be able to explain what the framework does for you.
How can I use AI tools without harming my learning?
Try the problem first, ask for a hint before a full answer, and explain every line you keep. Run the code and test unusual inputs. If you cannot rebuild it without the tool, you have not learned it yet.
Can I paste company or college data into a public chatbot?
Do not paste private data unless the owner and the rules of your company or college allow it. Remove names, phone numbers, keys and other personal details, or practise with made-up data instead.
How do I show these practices in a portfolio?
Put a clear README, a versioned prompt, a small test set and your results in the repository. Add one section on limits and safety. Never upload API keys or private data.
Build these habits with a trainer beside you. Book a free demo class, in Madhapur or live online, and see how a batch of 27 works on an AI project.
