Coding & Technical

Code Optimization Suggestions Report AI Prompt

Optimizing code is tough when you’re not sure what to target first. You might sense performance issues or see slow execution, but vague requests like “make this faster” rarely produce helpful results. Without clear context, AI tools miss root causes, skip constraints, or propose changes that don’t fit your codebase.

This is where a strong prompt makes all the difference. When you give the AI specific goals, constraints, and environment details, you get a focused optimization plan instead of generic advice. AskSmarter.ai helps you build prompts with the right level of clarity by asking smart questions about your codebase, performance goals, limits, and priorities.

With a refined prompt, you get actionable, relevant optimization recommendations that save you time and support cleaner code across your team.

intermediate9 min read

Why this is hard to get right

A Real Scenario: From Bottleneck to Breakthrough

Marcus is a backend engineer at a mid-sized logistics company. His team's data processing pipeline has been degrading for months. Jobs that once completed in under 10 minutes now take upward of 45 minutes, and the business is starting to notice.

He knows the problem lives somewhere in a core Python module — the one that handles batch record processing before writing to the database. But knowing the problem exists and knowing how to fix it are two very different things.

Marcus tries the obvious move: he pastes the function into an AI assistant and types, "Can you optimize this code?"

The response comes back fast. It looks helpful at first glance — suggestions about list comprehensions, a note about avoiding global variables, a generic recommendation to profile with cProfile. But none of it addresses his real constraint: the function runs in a tightly versioned environment (Python 3.10, no new dependencies without a security review), processes 50,000 records per minute, and must maintain backward compatibility with three downstream consumers.

The AI didn't know any of that. It couldn't. Marcus gave it nothing to work with beyond raw code.

He rewrites the request. This time he specifies that he needs a senior-engineer perspective, explains the data volume, names the Python version, and asks explicitly for tradeoff explanations. He also asks the AI to flag any suggestions that would require new library dependencies.

The difference is immediate. The AI identifies a specific bottleneck in how dictionaries are being constructed inside a loop — something the first response glossed over entirely. It proposes two alternative approaches, explains the memory vs. speed tradeoff between them, and notes that one approach requires Python 3.10's newer match syntax while the other is fully backward-compatible.

Marcus implements the backward-compatible approach in an afternoon. Pipeline runtimes drop from 45 minutes back to 9 minutes.

The lesson isn't that AI is smarter with more code. It's that AI performs better when you give it the same context you'd give a senior colleague you were asking for help. You wouldn't walk up to a coworker and say "fix this." You'd tell them what it does, what's broken, what constraints exist, and what success looks like.

That's exactly what a well-crafted optimization prompt does. It loads the AI with context so it can think like a specialist — not a generalist guessing in the dark.

Common mistakes to avoid

  • Pasting Code Without Explaining the Execution Context

    When you share code without describing where and how it runs, the AI optimizes in a vacuum. A function that handles 100 records behaves differently at 50,000 per minute under memory pressure. Always include throughput numbers, concurrency model, and deployment environment — these details reshape what counts as a good optimization.

  • Asking to 'Make It Faster' Without Defining Metrics

    Speed is relative. Without specifying whether you care about latency, throughput, CPU usage, or memory footprint, the AI picks a target arbitrarily. A suggestion that cuts execution time by 30% might triple memory usage. Define your primary performance goal and any secondary constraints before asking for recommendations.

  • Omitting Dependency and Compatibility Constraints

    AI tools will freely suggest third-party libraries or newer language features without knowing your constraints. This produces suggestions you can't actually use. State your Python version, banned dependencies, and compatibility requirements upfront — otherwise you'll spend time filtering out irrelevant recommendations.

  • Not Requesting Tradeoff Explanations

    Optimization always involves tradeoffs: speed vs. memory, readability vs. efficiency, simplicity vs. scalability. Without explicitly asking for tradeoffs, the AI presents recommendations as universally good. Ask the AI to explain what each suggestion costs — this turns a list of changes into a set of informed engineering decisions.

  • Ignoring Downstream Dependencies in the Prompt

    Code rarely lives alone. If the function you're optimizing feeds a message queue, exposes an API, or writes to a schema, refactoring its output structure could break consumers. Mention your upstream inputs and downstream consumers so the AI can scope recommendations that won't silently break adjacent systems.

  • Treating Output Formatting as Optional

    Without format guidance, the AI may return a discursive wall of text mixing code snippets, explanations, and caveats in no particular order. Ask for a numbered list, a table of recommendations with severity ratings, or inline annotated code — structured output makes review and prioritization dramatically faster.

The transformation

Before
Look at this code and tell me how to optimize it.
After
**Role:** You’re a senior software engineer.

**Task:** Review the provided Python function and propose **specific optimization improvements**.

**Context:** The function runs in a data processing pipeline handling 50k records per minute.

**Requirements:**
1. Highlight inefficiencies.
2. Suggest alternative algorithms or data structures.
3. Explain tradeoffs.

**Constraints:** Keep solutions compatible with Python 3.10.

**Output:** Produce a numbered list with clear, actionable recommendations.

Why this works

  • Role Assignment Sharpens Depth

    The prompt opens with 'You're a senior software engineer' — a deliberate role signal. This primes the AI to reason at an expert level, prioritize architectural concerns over surface-level fixes, and use the vocabulary of someone who has debugged production systems. Without this, responses tend toward introductory-level advice.

  • Workload Context Enables Relevant Recommendations

    The phrase '50k records per minute' does more than add color — it tells the AI exactly which class of problem this is. Recommendations appropriate for low-volume scripts differ sharply from those suited to high-throughput pipelines. This single data point redirects the AI toward vectorized operations, batching strategies, and memory efficiency.

  • Explicit Requirements Prevent Incomplete Responses

    The numbered Requirements list (inefficiencies, alternative algorithms, tradeoffs) ensures the AI doesn't stop at 'here's what's wrong.' It forces a complete analysis covering diagnosis, alternatives, and decision support. Without this structure, most AI responses diagnose the problem but skip the comparative analysis engineers need to act.

  • Version Constraint Filters Out Unusable Suggestions

    'Keep solutions compatible with Python 3.10' is a hard filter that prevents the AI from recommending features, syntax, or libraries that aren't available in the target environment. This small addition saves significant time — you don't have to manually evaluate every suggestion for compatibility.

  • Output Format Instruction Produces Actionable Deliverables

    The closing instruction to 'produce a numbered list with clear, actionable recommendations' transforms the AI's response from a prose analysis into an engineering checklist. Numbered outputs are easier to prioritize, delegate, and track — especially when sharing optimization findings with a team or documenting decisions in a PR.

The framework behind the prompt

The Theory Behind Optimization Prompts

Code optimization prompting sits at the intersection of prompt engineering and software performance analysis — and both disciplines reward specificity over generality.

Why Context Is the Core Constraint

In performance engineering, the classic framework is measure, profile, optimize — you never guess at bottlenecks. This same principle applies to AI prompting. When you ask for optimization without context, the AI defaults to pattern-matching against common problems it has seen in training data. It will suggest list comprehensions, early returns, and avoiding global variables — the optimization equivalent of telling someone to "just try harder."

When you provide workload data, environment constraints, and a measured baseline, you shift the AI from pattern-matching to constraint-satisfaction reasoning. It can now evaluate candidate solutions against your actual requirements, not hypothetical ones.

The RISEN Framework Applied to Technical Prompts

The RISEN framework (Role, Instructions, Steps, End goal, Narrowing) maps directly onto effective optimization prompts. The After Prompt on this page demonstrates each element:

  • Role: Senior software engineer (sets expert reasoning mode)
  • Instructions: Review and propose improvements (task scope)
  • Steps: Numbered requirements (process structure)
  • End goal: Actionable optimization plan (output purpose)
  • Narrowing: Python 3.10 compatibility, numbered list format (constraints and format)

Without the Narrowing layer — the constraints and output format — even a well-scoped optimization request produces unreliable results.

Algorithmic Complexity Awareness

Effective optimization prompts implicitly reference Big O complexity reasoning. When you tell the AI that your function handles 50,000 records per minute, you're flagging that O(n^2) behavior is catastrophic and O(n log n) solutions are worth the added complexity. Without scale context, the AI can't distinguish between a micro-optimization that saves 2ms and an algorithmic change that saves 40 seconds.

Chain-of-Thought as an Implicit Requirement

Asking the AI to explain tradeoffs is a form of Chain-of-Thought prompting — it forces the model to reason step by step rather than jumping to conclusions. Research consistently shows that prompts requiring visible reasoning steps produce more accurate and defensible outputs, particularly for complex technical tasks where the "right" answer depends on unstated constraints.

RISEN FrameworkChain-of-Thought PromptingFew-Shot PromptingCoSTAR

Prompt variations

Frontend Performance — JavaScript Bundle Optimization

Role: You are a senior frontend performance engineer.

Task: Review the provided JavaScript module and identify specific opportunities to reduce bundle size and improve runtime execution speed.

Context: This module is part of a React 18 application. The current bundle size for this module is 142KB (gzipped). First contentful paint is averaging 3.4 seconds on mid-tier mobile devices.

Requirements:

  1. Identify imports that could be tree-shaken or lazy-loaded.
  2. Flag synchronous operations that block the main thread.
  3. Suggest code-splitting strategies where applicable.
  4. Explain the expected impact of each recommendation in measurable terms (e.g., estimated KB reduction, milliseconds saved).

Constraints: No changes to the public API surface. Must remain compatible with Webpack 5 and Node 18.

Output: Return a prioritized list of recommendations ranked by estimated performance impact, highest first.

Database Query Optimization — SQL Performance Review

Role: You are a senior database engineer with deep expertise in PostgreSQL query optimization.

Task: Analyze the provided SQL query and execution plan, then recommend specific changes to reduce query execution time.

Context: This query runs on a PostgreSQL 15 database with approximately 8 million rows in the primary table. It currently takes 4.2 seconds to execute and runs roughly 200 times per hour during peak traffic.

Requirements:

  1. Identify missing or inefficient indexes.
  2. Flag any full table scans or Seq Scans that could be eliminated.
  3. Suggest query rewrites that preserve identical result sets.
  4. Note if any recommendations require maintenance windows or temporary locks.

Constraints: Read-only access to the schema — no structural table changes. Must remain compatible with PostgreSQL 15.

Output: Provide a numbered list of recommendations. For each one, include the expected improvement and any operational risk.

Memory Optimization — Python Data Science Workload

Role: You are a senior data engineer specializing in memory-efficient Python workloads.

Task: Review the provided pandas-based data transformation script and recommend changes to reduce peak memory consumption.

Context: This script processes CSV files averaging 2GB in size on a machine with 16GB RAM. The script currently peaks at 11GB memory usage, causing occasional OOM kills in the production environment. It runs once per hour as a scheduled job.

Requirements:

  1. Identify operations that create unnecessary in-memory copies of DataFrames.
  2. Suggest chunked processing or streaming alternatives where applicable.
  3. Recommend dtype optimizations that reduce memory footprint without losing precision.
  4. Flag any operations that could be replaced with more memory-efficient libraries.

Constraints: Python 3.11, pandas 2.0. No Spark or distributed compute — single-machine execution only.

Output: Return recommendations as a numbered list sorted by estimated memory reduction, with before/after memory estimates where calculable.

API Response Time — Node.js Async Bottleneck Review

Role: You are a senior Node.js backend engineer with expertise in async performance and I/O optimization.

Task: Review the provided Express.js route handler and identify the root causes of high p95 response latency.

Context: This endpoint handles product search requests. P95 latency is currently 1,800ms against a 400ms target. The service runs on Node.js 20 with 4 worker threads. The endpoint makes calls to two external services and one PostgreSQL database.

Requirements:

  1. Identify sequential async operations that could run in parallel.
  2. Flag missing or misconfigured caching opportunities.
  3. Highlight any N+1 query patterns in database interactions.
  4. Recommend connection pooling or batching improvements if applicable.

Constraints: Express.js 4.x, Node.js 20 LTS. No architectural changes to the service boundary — improvements must live within this route handler.

Output: Prioritized numbered list with expected latency impact per recommendation. Flag any changes that require load testing before production deployment.

When to use this prompt

  • Backend Engineers

    Identify performance bottlenecks in API handlers or batch jobs that slow down throughput.

  • Data Engineers

    Optimize Python data processing tasks that struggle with large input volumes.

  • Product Managers

    Get technical clarity on why certain features slow down and how to improve them.

  • Engineering Leads

    Standardize code optimization reviews across distributed teams.

Pro tips

  • 1

    Specify your runtime environment to get accurate optimization guidance.

  • 2

    Share realistic workload numbers so recommendations match real-world usage.

  • 3

    Define constraints early to avoid suggestions that won’t fit your codebase.

  • 4

    Request tradeoff explanations to make informed decisions.

A single optimization prompt rarely exhausts what's possible. Expert engineers use a layered prompting strategy — starting broad, then drilling down.

Round 1 — Diagnosis: Use the base prompt to get a full list of recommendations ranked by impact. Review the output and select the highest-priority fix.

Round 2 — Deep Dive: Prompt again with: 'Focus exclusively on recommendation #2 from your previous response. Provide a complete refactored implementation, explain each change line by line, and identify any edge cases the new implementation must handle.'

Round 3 — Validation Criteria: Prompt a third time: 'Given the refactored implementation above, write three unit test cases that specifically validate the performance-critical behavior. Include a benchmark test using Python's timeit module.'

This three-pass approach mirrors how senior engineers actually work — diagnose, implement, verify. Each round builds on the last, and you end up with not just a recommendation but a tested, documented change ready for code review.

You can also use a comparison prompt between two candidate implementations: 'Given implementations A and B below, compare their time complexity, space complexity, and readability. Recommend which to adopt for a high-throughput production workload and explain why.'

The core prompt structure works across engineering domains — but the context details that matter most differ significantly by discipline.

Data Engineering: Throughput (records/second), data skew, and serialization format (Parquet vs. CSV vs. JSON) are critical context. Mention your orchestration tool (Airflow, Prefect) and whether the job runs distributed or single-node.

Backend API Engineering: Latency percentiles (p50, p95, p99) matter more than averages. Include your concurrency model (threads, async, processes), connection pool settings, and whether you're behind a load balancer.

Frontend / JavaScript: Bundle size metrics, Core Web Vitals scores, and device tier targets (mid-range mobile vs. desktop) are the most relevant context. Mention your bundler (Webpack, Vite, Rollup) and whether SSR is in scope.

Embedded / Systems Engineering: Memory budget in KB, real-time constraints (hard vs. soft), interrupt handler safety, and compiler version are non-negotiable context items. Generic AI suggestions that assume unlimited heap are useless without these.

Machine Learning Inference: Batch size, latency SLA (synchronous vs. async inference), hardware target (CPU, GPU, edge device), and framework version (PyTorch, ONNX, TensorRT) all reshape what optimization looks like.

Always translate generic prompt fields into the vocabulary of your specific domain.

Use this checklist to verify your prompt is complete before submitting to any AI tool:

Context completeness:

  • Language and version specified (e.g., Python 3.10, Node 20 LTS)
  • Runtime environment described (cloud function, on-prem server, edge device)
  • Workload volume included (records/min, requests/sec, file size)
  • Current performance baseline stated (execution time, memory usage, latency)
  • Target performance goal defined (e.g., 'under 200ms p95')

Constraints documented:

  • Dependency restrictions noted (no new pip packages, no external APIs)
  • Compatibility requirements listed (downstream consumers, API surface)
  • Operational constraints included (no schema migrations, no maintenance windows)

Output instructions set:

  • Format requested (numbered list, annotated code, comparison table)
  • Prioritization requested (by impact, by effort, by risk)
  • Tradeoff explanations requested (speed vs. memory, readability vs. efficiency)

Code provided:

  • Smallest reproducible unit included (not entire codebase)
  • Relevant supporting context described (not pasted in full)
  • Any profiling data or benchmark results attached

If you can check every item above, your prompt will generate significantly more useful optimization recommendations than a generic request.

When not to use this prompt

When This Prompt Pattern Is Not the Right Fit

This optimization prompt structure works well for targeted, scoped code review — but it has real limitations you should know about.

Don't use it when you haven't profiled first. Asking AI to optimize code you haven't measured is a shortcut that often leads you to optimize the wrong thing. Run a profiler (cProfile, py-spy, or similar) and identify your actual hotspot before prompting. Otherwise, you might get a polished, faster version of code that isn't your bottleneck.

Avoid it for architectural decisions. If your performance problem requires rethinking service boundaries, switching databases, or moving to async processing at the system level, an optimization prompt for a single function won't help. Those decisions need a different prompt type — one focused on system design and architecture tradeoffs.

It's not a substitute for load testing. AI-suggested optimizations are hypotheses. They should be validated in your actual environment under realistic load. Never ship an AI-recommended performance change to production without benchmarking it first.

Skip it for security-sensitive refactoring. Optimization changes in authentication handlers, cryptographic routines, or permission checks carry security risk that outweighs performance gains. In those areas, correctness and auditability matter more than speed — and AI tools are not reliable security reviewers.

Troubleshooting

AI recommendations are too generic — suggestions like 'use list comprehensions' or 'avoid global variables' that don't address my actual bottleneck

Your prompt is missing workload context and a specific performance complaint. Add your current benchmark numbers and a description of the symptom: 'This function processes 50k records/min and takes 800ms per batch. Profiling shows 70% of time is spent in the inner loop.' This forces the AI to engage with your specific bottleneck rather than offering textbook advice.

AI keeps suggesting third-party libraries I can't install due to security policy

Strengthen the constraints section with a hard prohibition: 'Do not suggest any solution that requires installing new packages. The approved dependency list is limited to: [paste your current requirements.txt here].' Listing the exact approved libraries — not just the version — gives the AI a concrete boundary and redirects it toward pure-Python or in-stdlib solutions.

AI suggestions are technically correct but would break downstream consumers of this function

You haven't described the integration contract. Add a constraints line: 'This function's public signature, return type, and error behavior must remain identical — three downstream services depend on it.' If you can describe the downstream consumers briefly (e.g., 'a Celery worker reads the return dict by key name'), the AI will avoid refactors that change output structure.

AI produces a wall of text with no clear prioritization — I can't tell where to start

Your output instruction isn't specific enough. Replace a vague format request with: 'Return a numbered list of no more than 8 recommendations. Sort them by estimated impact-to-effort ratio, highest first. For each item, state: (1) the change, (2) expected improvement, (3) implementation effort — low/medium/high, and (4) any risks.' This structure forces the AI to do the prioritization work for you.

AI provides refactored code but doesn't explain what changed or why

Add an explicit explanation requirement to your requirements list. Specify: 'For each code change, include an inline comment explaining why the change improves performance. After the code block, provide a 2-3 sentence summary of the core optimization principle applied.' Without this instruction, AI tools default to showing you what changed but not teaching you the underlying reasoning.

How to measure success

How to Evaluate AI Output Quality for Optimization Prompts

Not all optimization responses are equally useful. Here's how to assess what you get back.

Strong output signals:

  • Specific line or pattern references — good responses cite the exact code construct causing the issue, not just a general principle
  • Measurable improvement estimates — claims like "this reduces time complexity from O(n^2) to O(n log n)" are verifiable; claims like "this will be faster" are not
  • Explicit tradeoff statements — any recommendation that doesn't acknowledge what it costs (memory, readability, compatibility) should be treated with skepticism
  • Constraint compliance — verify that every suggestion respects your stated Python version, dependency restrictions, and API surface requirements
  • Ranked or prioritized output — a flat list of 12 suggestions with no prioritization is harder to act on than a top-5 ranked by impact

Warning signs in AI output:

  • Generic advice not tied to your specific code
  • Suggestions requiring unlisted dependencies
  • Missing explanations for why a change improves performance
  • Recommendations that modify function signatures or return types without flagging the downstream risk

Now try it on something of your own

Reading about the framework is one thing. Watching it sharpen your own prompt is another — takes 90 seconds, no signup.

Turn your performance problem into a precise, constraint-aware optimization brief — ready to use with any AI tool.

Try one of these

Frequently asked questions

Include the smallest self-contained unit that demonstrates the problem — usually a single function or module, not an entire codebase. If context is essential (e.g., a helper class the function relies on), include a trimmed version. Pasting thousands of lines overwhelms the AI's context window and dilutes the focus. Isolate the bottleneck first, then prompt around that specific code.

Yes — profiling data dramatically improves recommendation quality. If you have output from cProfile, py-spy, or a flamegraph, include the top 10 hotspots. Even rough benchmark numbers help: 'this function takes 800ms on average, target is under 200ms' gives the AI a concrete goal to optimize toward rather than guessing at what 'fast enough' means.

Replace the language and version references throughout the prompt. For Go, specify the Go version and whether you're using goroutines. For Java, note the JVM version and whether you're targeting GraalVM. Also adjust the role: 'senior Go engineer' or 'Java performance specialist' signals different idiomatic knowledge. Keep the structure (role, task, context, requirements, constraints, output) — only the domain specifics change.

Strengthen your constraints section. Instead of listing only your Python version, explicitly state: 'Do not suggest any solution requiring new pip dependencies not already in this requirements.txt.' You can paste your current dependency list directly into the prompt. This hard boundary prevents the AI from recommending solutions that would require a security review or dependency approval process.

You can, but you'll get better results by summarizing the architecture rather than pasting all the code. Describe the call chain, note which file is the bottleneck, and provide only the hotspot code in full. For example: 'The pipeline has three stages — ingestion, transformation, and export. The bottleneck is in the transformation module, provided below.' This gives the AI structural context without overwhelming it.

Add a prioritization instruction to your output section. For example: 'Rank recommendations by estimated impact-to-effort ratio, highest first. Flag any changes that can be implemented in under 30 minutes.' This steers the AI toward quick wins rather than deep refactors — useful when you're optimizing under a deadline or sprint constraint.

Because it doesn't know your test coverage unless you tell it. Add a requirement: 'All suggestions must preserve the existing public function signature and return type.' If you have specific test cases that define correct behavior, describe them briefly. This anchors the AI's suggestions to behavioral correctness, not just performance metrics.

Your turn

Build a prompt for your situation

This example shows the pattern. AskSmarter.ai guides you to create prompts tailored to your specific context, audience, and goals.