AIExplore
Ask ChatGPT for Working Code With Context
Name the language, runtime, and expected behavior so the code fits your project instead of a generic textbook example.
It is easy to type write a function that does X and paste whatever comes back. It runs on the first try often enough that the habit sticks. Then one day the code silently fails, or targets the wrong runtime, or uses a library your project does not have. The root cause is almost always missing context.
Working code is code that fits your project. That means the right language, the right runtime, the right dependencies, and the right assumptions about inputs and outputs. A short brief at the top of the request makes ChatGPT aim at your project instead of a generic example from a tutorial.
What context really includes
Context is more than the language name. It covers the runtime, the framework, the version, the input shape, the expected output, and the edge cases you already know about. Each piece narrows the space of acceptable answers. Skip any of them and ChatGPT fills in a default that may not match yours.
When context matters most
- Building a function that will run in production
- Wiring code into an existing service with a specific framework
- Writing a script that has to run on a specific runtime version
- Reading or writing files where format and encoding matter
- Handling money, dates, or user input where edge cases are common
Prompt for a small function with clear context
Task: write a function that formats a phone number for display. Language: TypeScript. Runtime: Node 20. Input: string, may contain digits and any of +, spaces, dashes, parentheses. Output: string in the format +CC XXX XXX XXXX for a valid E.164 number. Invalid input: return the original string unchanged. Include JSDoc comments and three example calls in a comment block at the bottom. Do not add any external libraries.
Why this prompt works
It names the language, runtime, exact input, exact output, invalid handling, and a rule about dependencies. That is more than most requests carry, and it is why the reply usually runs on the first try inside a real project.
Prompt for a script that touches files
Task: write a Python 3.11 script that renames image files in a folder. Input: a folder path passed as argv[1]. Behavior: rename any file ending in .jpeg to .jpg, print each rename as "old -> new". Do not touch other files. Do not descend into subfolders. On conflict where the target name already exists, skip and log a warning. Use only the standard library.
Prompt for a small web request handler
Task: write an Express route handler.
Language: JavaScript.
Framework: Express 4 on Node 20.
Endpoint: POST /webhook.
Input: JSON body with fields id (string) and status (one of "open", "closed").
Behavior: validate the body, then respond 200 with { ok: true } or 400 with { ok: false, error }.
Do not add external validation libraries. Include a short comment explaining the validation choice.What to include in every code request
- Language and runtime with a version if it matters
- Framework or platform if relevant
- Exact input shape and expected output
- Error and edge case behavior you already know about
- Rules about dependencies, formatting, or comments
How to refine when the first version misses
Do not throw it out. Name the mismatch and ask for a small fix. For example, the return type should be Promise of string not string, please adjust that and leave everything else alone. Small named fixes protect the parts of the code that were already right.
Common mistakes
- Skipping the runtime and getting code for a version you do not use
- Not naming edge cases so the reply silently ignores them
- Accepting an external library you did not ask for
- Copying code without running it in your project
- Adding many changes in the same round instead of one at a time
How to check the result
Run the code with a real input from your project. Then test one edge case, such as an empty value or an invalid type. If both pass, read the function once and confirm it does not touch anything you did not intend. That short round of checks catches most subtle problems.
Takeaway
Working code fits a real project. Name the language, runtime, inputs, outputs, and edge cases, and ChatGPT stops writing textbook examples.

explore