How to Use AI Agents to Optimize Code

Explore top LinkedIn content from expert professionals.

Summary

AI agents are computer programs that use artificial intelligence to analyze, write, and improve code by mimicking teamwork or step-by-step problem solving. Using AI agents to refine code involves giving them context, prompting them to review and revise, and treating them as collaborative partners in your development workflow.

  • Define clear roles: Assign each AI agent a specific task—such as writing, reviewing, or fixing code—to create a structured workflow with fresh perspectives.
  • Use structured feedback: Encourage agents to critique their own or each other's code and automatically incorporate constructive suggestions, leading to higher-quality results.
  • Integrate tools and memory: Let agents access your codebase, run tests, and remember past changes so they can handle complex tasks and adapt over multiple sessions.
Summarized by AI based on LinkedIn member posts
BERJAYA BERJAYA BERJAYA
  • View profile for Sahar Mor

    I help researchers and builders make sense of AI | ex-Stripe | aitidbits.ai | Angel Investor

    42,016 followers

    Recently, I adopted a coding tip from the Anthropic team that has significantly boosted the quality of my AI-generated code. Anthropic runs multiple Claude instances in parallel to dramatically improve code quality compared to single-instance workflows. How it works: (1) One Claude writes the code, the coder - focusing purely on implementation (2) A second Claude reviews it, the reviewer - examining with fresh context, free from implementation bias (3) A third Claude applies fixes, the fixer - integrating feedback without defensiveness This technique works with any AI assistant, not just Claude. Spin each agent up in its own tab—Cursor, Windsurf, or plain CLI. Then, let Git commits serve as the hand-off protocol. This separation mimics human pair programming but supercharges it with AI speed. When a single AI handles everything, blind spots emerge naturally. Multiple instances create a system of checks and balances that catch what monolithic workflows miss. This shows that context separation matters. By giving each AI a distinct role with clean context boundaries, you essentially create specialized AI engineers, each bringing a unique perspective to the problem. This and a dozen more tips for developers building with AI in my latest AI Tidbits post https://lnkd.in/gTydCV9b

  • View profile for Aadit Sheth

    Co-founder, The Narrative Company. Storytelling and comms for world-class companies on X & LinkedIn

    98,376 followers

    Everyone thinks AI coding agents can replace engineers. This is wrong. Here’s how to actually make them useful: Agents generate code that almost works. Engineers close the loop: prompt → review → edit → test → repeat. The teams that win are the ones who make that loop fast and tight. Most teams are still stuck in “ask once, hope it compiles” habits. But here’s what actually works now: 1. Give agents real context → Keep a project guide (agents[.]md, rules[.]md, etc.) → Define goals, edge cases, and examples → Make it clear what “done” looks like before asking for code 2. Treat agents like junior devs → First: ask them to explain changes → Turn that into a clear checklist → Decide how much autonomy they get each run 3. Stay inside a diff-first loop → Always review diffs before merging → Separate shell commands from prompts → Reference files directly to tighten context 4. Edit small things yourself → Quick copy or logic fixes are faster by hand → Recompile and test right after each accepted diff → Let the agent continue in parallel on the next task 5. Capture what you learn → Document tricky fixes for reuse → Save commands, patterns, and notes → Every iteration gets faster with history 6. Ship with guardrails → Commit and PR from the terminal → Keep human review in the loop → Ship confidently, not blindly Agents don’t replace engineers. They amplify engineers who know how to run a tight feedback loop. Here's the link if you want to start building: https://go.warp.dev/aadit

  • View profile for Andrew Ng
    Andrew Ng Andrew Ng is an Influencer

    DeepLearning.AI, AI Fund and AI Aspire

    2,499,670 followers

    Last week, I described four design patterns for AI agentic workflows that I believe will drive significant progress: Reflection, Tool use, Planning and Multi-agent collaboration. Instead of having an LLM generate its final output directly, an agentic workflow prompts the LLM multiple times, giving it opportunities to build step by step to higher-quality output. Here, I'd like to discuss Reflection. It's relatively quick to implement, and I've seen it lead to surprising performance gains. You may have had the experience of prompting ChatGPT/Claude/Gemini, receiving unsatisfactory output, delivering critical feedback to help the LLM improve its response, and then getting a better response. What if you automate the step of delivering critical feedback, so the model automatically criticizes its own output and improves its response? This is the crux of Reflection. Take the task of asking an LLM to write code. We can prompt it to generate the desired code directly to carry out some task X. Then, we can prompt it to reflect on its own output, perhaps as follows: Here’s code intended for task X: [previously generated code] Check the code carefully for correctness, style, and efficiency, and give constructive criticism for how to improve it. Sometimes this causes the LLM to spot problems and come up with constructive suggestions. Next, we can prompt the LLM with context including (i) the previously generated code and (ii) the constructive feedback, and ask it to use the feedback to rewrite the code. This can lead to a better response. Repeating the criticism/rewrite process might yield further improvements. This self-reflection process allows the LLM to spot gaps and improve its output on a variety of tasks including producing code, writing text, and answering questions. And we can go beyond self-reflection by giving the LLM tools that help evaluate its output; for example, running its code through a few unit tests to check whether it generates correct results on test cases or searching the web to double-check text output. Then it can reflect on any errors it found and come up with ideas for improvement. Further, we can implement Reflection using a multi-agent framework. I've found it convenient to create two agents, one prompted to generate good outputs and the other prompted to give constructive criticism of the first agent's output. The resulting discussion between the two agents leads to improved responses. Reflection is a relatively basic type of agentic workflow, but I've been delighted by how much it improved my applications’ results. If you’re interested in learning more about reflection, I recommend: - Self-Refine: Iterative Refinement with Self-Feedback, by Madaan et al. (2023) - Reflexion: Language Agents with Verbal Reinforcement Learning, by Shinn et al. (2023) - CRITIC: Large Language Models Can Self-Correct with Tool-Interactive Critiquing, by Gou et al. (2024) [Original text: https://lnkd.in/g4bTuWtU ]

  • View profile for Andreas Horn

    Head of AIOps @ IBM || Speaker | Lecturer | Advisor

    244,147 followers

    Anthropic 𝗷𝘂𝘀𝘁 𝗿𝗲𝗹𝗲𝗮𝘀𝗲𝗱 𝗮 𝗱𝗲𝗻𝘀𝗲 𝗮𝗻𝗱 𝗵𝗶𝗴𝗵𝗹𝘆 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗿𝗲𝗽𝗼𝗿𝘁 𝗼𝗻 𝗵𝗼𝘄 𝘁𝗼 𝗯𝘂𝗶𝗹𝗱 𝗲𝗳𝗳𝗲𝗰𝘁𝗶𝘃𝗲 𝗔𝗜 𝗮𝗴𝗲𝗻𝘁𝘀 — 𝗽𝗮𝗰𝗸𝗲𝗱 𝘄𝗶𝘁𝗵 𝗲𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴 𝗶𝗻𝘀𝗶𝗴𝗵𝘁𝘀 𝗳𝗿𝗼𝗺 𝗿𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝗱𝗲𝗽𝗹𝗼𝘆𝗺𝗲𝗻𝘁𝘀: ⬇️ Not just marketing, BUT a real, practical blueprint for developers and teams building AI agents that actually work. It explains how Claude Code (tool for agentic coding) can function as a software developer: writing, reviewing, testing, and even managing Git workflows autonomously. BUT in my view: The principles and patterns described in this document are not Claude-specific. You can apply them to any coding agent — from OpenAI’s Codex to Goose, Aider, or even tools like Cursor and GitHub Copilot Workspace. 𝗛𝗲𝗿𝗲 𝗮𝗿𝗲 7 𝗸𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁𝘀 𝗳𝗼𝗿 𝗯𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗯𝗲𝘁𝘁𝗲𝗿 𝗔𝗜 𝗮𝗴𝗲𝗻𝘁𝘀 — 𝘁𝗵𝗮𝘁 𝘄𝗼𝗿𝗸 𝗶𝗻 𝘁𝗵𝗲 𝗿𝗲𝗮𝗹 𝘄𝗼𝗿𝗹𝗱: ⬇️ 1. 𝗔𝗴𝗲𝗻𝘁 𝗱𝗲𝘀𝗶𝗴𝗻 ≠ 𝗷𝘂𝘀𝘁 𝗽𝗿𝗼𝗺𝗽𝘁𝗶𝗻𝗴 ➜ It’s not about clever prompts. It’s about building structured workflows — where the agent can reason, act, reflect, retry, and escalate. Think of agents like software components: stateless functions won’t cut it. 2. 𝗠𝗲𝗺𝗼𝗿𝘆 𝗶𝘀 𝗮𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲 ➜ The way you manage and pass context determines how useful your agent becomes. Using summaries, structured files, project overviews, and scoped retrieval beats dumping full files into the prompt window. 3. 𝗣𝗹𝗮𝗻𝗻𝗶𝗻𝗴 𝗶𝘀𝗻’𝘁 𝗼𝗽𝘁𝗶𝗼𝗻𝗮𝗹 ➜ You can’t expect an agent to solve multi-step problems without an explicit process. Patterns like plan > execute > review, tool use when stuck, or structured reflection are necessary. And they apply to all models, not just Claude. 4. 𝗥𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝗮𝗴𝗲𝗻𝘁𝘀 𝗻𝗲𝗲𝗱 𝗿𝗲𝗮𝗹-𝘄𝗼𝗿𝗹𝗱 𝘁𝗼𝗼𝗹𝘀 ➜ Shell access. Git. APIs. Tool plugins. The agents that actually get things done use tools — not just language. Design your agents to execute, not just explain. 5. 𝗥𝗲𝗔𝗰𝘁 𝗮𝗻𝗱 𝗖𝗼𝗧 𝗮𝗿𝗲 𝘀𝘆𝘀𝘁𝗲𝗺 𝗽𝗮𝘁𝘁𝗲𝗿𝗻𝘀, 𝗻𝗼𝘁 𝗺𝗮𝗴𝗶𝗰 𝘁𝗿𝗶𝗰𝗸𝘀 ➜ Don’t just ask the model to “think step by step.” Build systems that enforce that structure: reasoning before action, planning before code, feedback before commits. 6. 𝗗𝗼𝗻’𝘁 𝗰𝗼𝗻𝗳𝘂𝘀𝗲 𝗮𝘂𝘁𝗼𝗻𝗼𝗺𝘆 𝘄𝗶𝘁𝗵 𝗰𝗵𝗮𝗼𝘀 ➜ Autonomous agents can cause damage — fast. Define scopes, boundaries, fallback behaviors. Controlled autonomy > random retries. 7. 𝗧𝗵𝗲 𝗿𝗲𝗮𝗹 𝘃𝗮𝗹𝘂𝗲 𝗶𝘀 𝗶𝗻 𝗼𝗿𝗰𝗵𝗲𝘀𝘁𝗿𝗮𝘁𝗶𝗼𝗻 ➜ A good agent isn’t just a wrapper around an LLM. It’s an orchestrator: of logic, memory, tools, and feedback. And if you’re scaling to multi-agent setups — orchestration is everything. Check the comments for the original material! Enjoy! Save 💾 ➞ React 👍 ➞ Share ♻️ & follow for everything related to AI Agents!

  • View profile for Greg Coquillo
    Greg Coquillo Greg Coquillo is an Influencer

    AI Infrastructure Product Leader | Scaling GPU Clusters for Frontier Models | Microsoft Azure AI & HPC | Former AWS, Amazon | Startup Investor | Linkedin Top Voice | I build the infrastructure that allows AI to scale

    230,643 followers

    AI coding agents feel simple when you use them- but behind the scenes, a lot more is happening than just generating code. They’re not just responding… they’re understanding, planning, executing, and adapting in real time. That’s the shift most people miss. Once you see how they actually work, you stop treating them like tools… and start using them like systems. Here’s what’s really happening inside a coding AI agent 👇 🔹 Live Repo Context The agent reads your entire codebase - files, dependencies, Git state, configs-  so it works with real context instead of guessing. 🔹 Prompt Structure & Cache Reuse Separates stable instructions from dynamic inputs, reuses context, and reduces token usage for faster, consistent responses. 🔹 Tool Access & Execution This is where things get powerful- running code, reading files, executing commands, and interacting with real systems. 🔹 Context Optimization Filters out noise by trimming logs, removing duplicates, and prioritizing relevant information. 🔹 Structured Memory System Remembers past steps, tasks, and decisions- enabling long, multi-step workflows without losing context. 🔹 Subagents & Delegation Breaks complex tasks into smaller ones, handled in parallel by specialized agents for speed and scalability. Put all of this together… and you’re not just interacting with a model. You’re working with a system that can: → Understand your codebase → Plan tasks step-by-step → Execute real actions → Remember context across sessions → Solve complex problems efficiently The biggest shift? Moving from “Ask AI a question” to “Collaborate with an AI system.” Because this is where things are heading, AI agents that don’t just assist… They operate inside your workflow like an actual developer.

  • View profile for Donn Felker

    Software Professional and Author

    6,542 followers

    A lot of people say that AI agents don't work well on large legacy codebases. That's false. Over the last eight months, I've been successfully using AI agents to clean up massive portions of legacy tech debt in a very large Java and Kotlin codebase that's over 10 years old and used by millions of people. It works. You just have to set up the prompt and engineer the context management correctly. The prompts I use have been iterated on for quite a while. I create one, test it out, find some edge cases I missed, undo the changes, and try again. I do this while performing other work, so it's done in parallel. Ultimately, the prompt typically ends up anywhere from around 800 words to over 2,000 words long. It's a basic full-on manual of how to perform some type of tech debt migration. This has been further expanded by other skills that I have developed. The agent will call into them when it needs to perform certain actions or determines that it is not ready to perform a particular operation because of another dependency. So they maintain their own context window. Done correctly and at the right level, you will have presented the agent with a set of instructions that is a full decision matrix. This covers how to successfully dissect, update, and migrate from one technology to another—all with full guardrails, verification, and more. The majority of pull requests I have opened with these agents are nearly impossible to identify as having been written by an agent. They follow proper standards, protocols, and implementation details that our team has dictated. This is enforced through deterministic methods such as linting, testing, verification, and sub-agents that do additional verification before completing. Finally, when the agent is complete and everything is working, I will open the pull request in a draft state and manually review it there. Once I'm happy with it, I'll transition it to a pull request that's ready to be reviewed. Then, additional AI agent reviewers will perform another set of analysis on top of that, along with a human reviewer.

  • View profile for Yu (Jason) Gu, PhD

    VP, Head of AI at Visa | Built $1B+ Enterprise AI Business | Planetary-Scale Systems (1.6B+ Daily Transactions) | Agentic AI & Responsible AI

    9,832 followers

    𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝘄𝗶𝘁𝗵 𝗔𝗜 𝗖𝗼𝗱𝗶𝗻𝗴 𝗧𝗼𝗱𝗮𝘆: You prompt → AI writes code → You ship → You start from zero. Every. Single. Time. This is why most developers plateau. They treat AI like chat bots. Top performers do something different: 𝗖𝗼𝗺𝗽𝗼𝘂𝗻𝗱 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴. ━━━━━━━━━━━━━━━━━━━━ 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗶𝘁? Building AI systems with memory. → Every PR educates the system → Every bug becomes a permanent lesson → Every code review updates agent behavior Regular AI coding makes you productive 𝘁𝗼𝗱𝗮𝘆. Compound Engineering makes you better 𝗲𝘃𝗲𝗿𝘆 𝗱𝗮𝘆 𝗮𝗳𝘁𝗲𝗿. ━━━━━━━━━━━━━━━━━━━━ 𝟰 𝗔𝗰𝘁𝗶𝗼𝗻𝘀 𝘁𝗼 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁: 𝟭. 𝗖𝗼𝗱𝗶𝗳𝘆 𝗬𝗼𝘂𝗿 𝗘𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲 Create AGENTS.md or .cursorrules in your repo. Document patterns, pitfalls, and PR references. This becomes your AI's "onboarding doc." 𝟮. 𝗠𝗮𝗸𝗲 𝗕𝘂𝗴𝘀 𝗣𝗮𝘆 𝗗𝗶𝘃𝗶𝗱𝗲𝗻𝗱𝘀 When fixing bugs, ask: Can a lint rule prevent this? Should AGENTS.md document it? A true fix ensures the agent never repeats it. 𝟯. 𝗘𝘅𝘁𝗿𝗮𝗰𝘁 𝗥𝗲𝘃𝗶𝗲𝘄 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀 Every review comment is a potential system upgrade. Turn feedback into reusable standards the agent auto-applies. 𝟰. 𝗕𝘂𝗶𝗹𝗱 𝗥𝗲𝘂𝘀𝗮𝗯𝗹𝗲 𝗪𝗼𝗿𝗸𝗳𝗹𝗼𝘄𝘀 Document task sequences. Next time: "Follow the add API endpoint workflow." The system already knows what to do. ━━━━━━━━━━━━━━━━━━━━ 𝗧𝗵𝗲 𝗖𝗼𝗺𝗽𝗼𝘂𝗻𝗱 𝗘𝗳𝗳𝗲𝗰𝘁 Imagine the AI saying: "Naming updated per PR #234. Over-testing removed per PR #219 feedback." It learned your taste—like a smart colleague with receipts. ━━━━━━━━━━━━━━━━━━━━ 𝗧𝗵𝗲 𝗟𝗲𝘃𝗲𝗿𝗮𝗴𝗲 𝗛𝗶𝗲𝗿𝗮𝗿𝗰𝗵𝘆 Bad code = one line affected Bad AGENTS.md instruction = 𝗲𝘃𝗲𝗿𝘆 𝘀𝗲𝘀𝘀𝗶𝗼𝗻 affected Treat agent config like production code. Highest-ROI investment you can make. ━━━━━━━━━━━━━━━━━━━━ Stop treating AI interactions as disposable. Start treating them as investments. That's how you go from "AI User" to "𝗔𝗜 𝗠𝘂𝗹𝘁𝗶𝗽𝗹𝗶𝗲𝗿." What's one pattern you've compounded into your AI workflow? 👇 #AgenticCoding #SoftwareEngineering #TechLeadership #GenAI #DeveloperProductivity

  • We've been thinking a lot about a workflow problem that every serious AI team faces now: How do you use agents to improve agents efficiently? Right now, when a coding agent needs to debug an AI product, the default in most cases is to try to pull down thousands of production traces and pattern-match its way to a root cause. That's slow and expensive at best. Kind of like handing someone a phone book and asking them to find the interesting people... Our new Insights agent was built to fix that. Rather than wait for a request, Freeplay's agent runs in the background, looking at production evaluator scores and reasoning traces on a schedule. It then clusters patterns, ranks issues by impact, and links each one to specific traces that show the problem. This is the kind of infrastructure coding agents need to work efficiently -- pre-computed analysis and signal so they can debug fast, not on-demand queries. Now when you ask Claude Code "what's wrong with our agent?", the Freeplay MCP server doesn't hand back 10,000 traces. It returns the top insights that matter this week, with the 50 traces that demonstrate them. The agent can know immediately what to look for before it reads a single log line. The old way: dig through lots of logs, hope you spot something. New way: start with the diagnosis, then dig deeper to decide how to fix it. This same workflow also helps every human user who logs into Freeplay. Dashboards and metrics help, but Insights tell you where to look much faster. We wrote up how it works and where it fits in the broader data flywheel to continuously improve an agent. Check out the video (shoutout to Jeremy Silva), and the link in the comments.

Explore categories