How to Create a Self Healing Code Agent

LangChain · Intermediate ·🧠 Large Language Models ·1y ago

Key Takeaways

The video demonstrates how to create a self-healing code agent using LangChain, leveraging open-source tools like Open Evals for static analysis and reflection steps to validate and improve generated code.

Full Transcript

Hi, this is Katherine from Lang. One of the most common use cases we see with LMS is code generation. But here's the question. How do you improve the accuracy and performance of the code generated by your code agent? One answer is static analysis. Checking the code for errors or for inconsistencies without executing it. This is where tools like Pyate or My Pi comes in. So now you could actually build that kind of check into your agent loop. This is what we call a reflection step. A point in the loop where the agent looks back at what it just generated and validates or improves it before continuing. We'll show you an example of how you can create such a reflection step in your architecture, evaluating its own output before returning a result. Instead of building evaluation logic from scratch, we'll show you how you can leverage open evals, an open- source package that provides you with out- of-the-box tools for things like type checking and sandbox evaluation. Open evals includes a number of helpful out ofthe-box evaluators. This includes ones form as a judge, rack, and code evaluation. Today, we're going to focus specifically on the code evaluator section. Open evals gives you several pre-built evaluators for generated code such as type-checking generated code, sandbox type checking and execution evaluators, and finally using Elm as a judge to evaluate code quality. Since LM outputs often contain both code and non-code text, open evaluators also include flexible code extraction utilities. You can either use an LLM to extract relevant snippets or simply extract markdown code blocks directly. Now let's deep dive into the several techniques that we can take to validate code. First, you can type check generated code with Pyite and my PI. These are lightweight approaches to type check Python code, but they won't install or check for missing dependencies. For TypeScript, there's a similar evaluator for type checking. Second, you can use LM as a judge to prompt LM to provide feedback on your code. LM as a judge evaluator here defaults to no code extraction strategy, but it gives the option to enable extracting code, which could be helpful to reduce noise or distraction, especially when using smaller models. Finally, open evals integrates with E2B to run some code evaluators in isolated sandboxes. Static analysis is a useful tool for caching mistakes in generated code. Open evals lets you perform this fully locally. Sometimes generated code can contain and misuse third party libraries. So rather than installing or executing these arbitrary dependencies locally, you can use the sandbox environment to effectively perform type checking. There are two key sandbox evaluators. First is the sandbox typeing evaluators. One example is the sandbox pyate. This evaluator parses out the required dependencies, installs them in a sandbox, and runs pyate. It returns any type-checking errors found for TypeScript. Similarly, there's a type-checking sandbox evaluator. The second type of sandbox evaluator is the sandbox execution evaluator. This evaluator goes a step beyond installing dependencies, actually running the code inside the sandbox and checking for runtime errors. Now let's take a look at the example we saw earlier to see how it's applied in action. Now back to the example we saw in the beginning. The base agent has a simple React architecture implementation where it works off of a text file embedded in the system prompt and it has tools to fetch further information to improve the correctness of generated code. Mini chat link chain includes a reflection set which verifies the generated code through the sandbox typeing evaluator in the loop. Let's take a look at how it's constructed. So this is our reflection node. In our reflection step, we first create a sandbox pyite co-evaluator where the evaluator extracts generated code from the agent's output through markdown and pushes it up to an E2B sandbox to run Pyrite over it. We obtained a result from running the evaluator. And now there are two situations where we will want to directly output the response to the end user. The first scenario is if the code generated does not have any type-checking issues. That is within the result score field it outputs true. So it wouldn't need any code regeneration. The second scenario is if the original agent response did not include any code at all. We can check for this by checking the result metadata to see if code extraction step in the evaluator field. In this case, we also want to directly output our agents response. So now transitioning that logic into code in the case that our agent's response contains code and finds false evaluator results. We want to feed the error message back to the agent to regenerate code. We do so by attaching a user message to the LM giving context on the errors found and directions to regenerate code. With this information in the prompt, the agent then has to inform context to regenerate and modify based on the error. Let's try this out. I can feed an example input to our agent asking to create a swarm style agent. The query is routed to our agent node which generates its respond while calling a set of tools. Once the response is finished generating, this is passed to a reflection node to create the results. Looks like the reflection node has caught some errors which is now prompting the agent to regenerate the code. Now our agent regenerates the response which now passes the evaluator test and their outputs our final answer. We can also inspect the trace and lang to take a detailed look at the run behavior. So first we can see that the base agent called a get langraph docs content tool. This fetches relevant content from relevant documents and then it calls the LLM to generate a response. The response is then passed to the reflection step where the evaluator is run. Taking a closer look at the evaluator results. It passed through a score of false and based on the error message several required arguments seems to be missing. So this includes model name, timeout and stop. So this is then passed on as a part of the human message. If we scroll down where we included the error found and asking the agent to try to fix it. So now having the context of the error message, the agent generates another round of outputs. The generated output is then passed to the reflection step. And during the second time of running the evaluator, we can see that no errors were found returning true. And from there, the final response is passed on to the customer. Adding this reflection step within our architecture. This helps catch some of the type checking mistakes that are being made by the LM and this can ensure better code quality for our agent. Thank you for watching.

Original Description

Code generation is one of the most common use cases we see with LLMs, but how do you improve the accuracy and performance of the code generated by your code agent? In this video, we demonstrate how you can create a reflection step in your architecture to validate or improve its generated code, before returning a response. OpenEvals: https://github.com/langchain-ai/openevals Mini Chat LangChain: https://github.com/jacoblee93/mini-chat-langchain/tree/main 0:00 Introduction 1:00 OpenEvals code evaluator introduction 3:32 Example implementation of reflection step in the loop - Mini Chat LangChain
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LangChain · LangChain · 0 of 60

← Previous Next →
1 Chat With Your Documents Using LangChain + JavaScript
Chat With Your Documents Using LangChain + JavaScript
LangChain
2 LangChain SQL Webinar
LangChain SQL Webinar
LangChain
3 LangChain "OpenAI functions" Webinar
LangChain "OpenAI functions" Webinar
LangChain
4 LangSmith Launch
LangSmith Launch
LangChain
5 LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain
6 LangChain Expression Language
LangChain Expression Language
LangChain
7 Building LLM applications with LangChain with Lance
Building LLM applications with LangChain with Lance
LangChain
8 Benchmarking Question/Answering Over CSV Data
Benchmarking Question/Answering Over CSV Data
LangChain
9 LangChain "RAG Evaluation" Webinar
LangChain "RAG Evaluation" Webinar
LangChain
10 Fine-tuning in Your Voice Webinar
Fine-tuning in Your Voice Webinar
LangChain
11 Tabular Data Retrieval
Tabular Data Retrieval
LangChain
12 Building an LLM Application with Audio by AssemblyAI
Building an LLM Application with Audio by AssemblyAI
LangChain
13 Superagent Deepdive Webinar
Superagent Deepdive Webinar
LangChain
14 Lessons from Deploying LLMs with LangSmith
Lessons from Deploying LLMs with LangSmith
LangChain
15 Shortwave Assistant Deepdive Webinar
Shortwave Assistant Deepdive Webinar
LangChain
16 Cognitive Architectures for Language Agents
Cognitive Architectures for Language Agents
LangChain
17 Effectively Building with LLMs in the Browser with Jacob
Effectively Building with LLMs in the Browser with Jacob
LangChain
18 Data Privacy for LLMs
Data Privacy for LLMs
LangChain
19 "Theory of Mind" Webinar with Plastic Labs
"Theory of Mind" Webinar with Plastic Labs
LangChain
20 LangChain Templates
LangChain Templates
LangChain
21 Using Natural Language to Query Postgres with Jacob
Using Natural Language to Query Postgres with Jacob
LangChain
22 Building a Research Assistant from Scratch
Building a Research Assistant from Scratch
LangChain
23 Benchmarking RAG over LangChain Docs
Benchmarking RAG over LangChain Docs
LangChain
24 Skeleton-of-Thought: Building a New Template from Scratch
Skeleton-of-Thought: Building a New Template from Scratch
LangChain
25 Benchmarking Methods for Semi-Structured RAG
Benchmarking Methods for Semi-Structured RAG
LangChain
26 LangSmith Highlights: Getting Started
LangSmith Highlights: Getting Started
LangChain
27 LangSmith Highlights: Debugging
LangSmith Highlights: Debugging
LangChain
28 LangSmith Highlights: Datasets
LangSmith Highlights: Datasets
LangChain
29 LangSmith Highlights: Evaluation
LangSmith Highlights: Evaluation
LangChain
30 LangSmith Highlights: Human Annotation
LangSmith Highlights: Human Annotation
LangChain
31 LangSmith Highlights: Monitoring
LangSmith Highlights: Monitoring
LangChain
32 LangSmith Highlights: Hub
LangSmith Highlights: Hub
LangChain
33 SQL Research Assistant
SQL Research Assistant
LangChain
34 Getting Started with Multi-Modal LLMs
Getting Started with Multi-Modal LLMs
LangChain
35 Build a Full Stack RAG App With TypeScript
Build a Full Stack RAG App With TypeScript
LangChain
36 Auto-Prompt Builder (with Hosted LangServe)
Auto-Prompt Builder (with Hosted LangServe)
LangChain
37 LangChain v0.1.0 Launch: Introduction
LangChain v0.1.0 Launch: Introduction
LangChain
38 LangChain v0.1.0 Launch: Observability
LangChain v0.1.0 Launch: Observability
LangChain
39 LangChain v0.1.0 Launch: Integrations
LangChain v0.1.0 Launch: Integrations
LangChain
40 LangChain v0.1.0 Launch: Composability
LangChain v0.1.0 Launch: Composability
LangChain
41 LangChain v0.1.0 Launch: Streaming
LangChain v0.1.0 Launch: Streaming
LangChain
42 LangChain v0.1.0 Launch: Output Parsing
LangChain v0.1.0 Launch: Output Parsing
LangChain
43 LangChain v0.1.0 Launch: Retrieval
LangChain v0.1.0 Launch: Retrieval
LangChain
44 LangChain v0.1.0 Launch: Agents
LangChain v0.1.0 Launch: Agents
LangChain
45 Build and Deploy a RAG app with Pinecone Serverless
Build and Deploy a RAG app with Pinecone Serverless
LangChain
46 Hosted LangServe + LangChain Templates
Hosted LangServe + LangChain Templates
LangChain
47 LangGraph: Intro
LangGraph: Intro
LangChain
48 LangGraph: Agent Executor
LangGraph: Agent Executor
LangChain
49 LangGraph: Chat Agent Executor
LangGraph: Chat Agent Executor
LangChain
50 LangGraph: Human-in-the-Loop
LangGraph: Human-in-the-Loop
LangChain
51 LangGraph: Dynamically Returning a Tool Output Directly
LangGraph: Dynamically Returning a Tool Output Directly
LangChain
52 LangGraph: Respond in a Specific Format
LangGraph: Respond in a Specific Format
LangChain
53 LangGraph: Managing Agent Steps
LangGraph: Managing Agent Steps
LangChain
54 LangGraph: Force-Calling a Tool
LangGraph: Force-Calling a Tool
LangChain
55 LangGraph: Multi-Agent Workflows
LangGraph: Multi-Agent Workflows
LangChain
56 Streaming Events: Introducing a new `stream_events` method
Streaming Events: Introducing a new `stream_events` method
LangChain
57 Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
LangChain
58 OpenGPTs
OpenGPTs
LangChain
59 Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
LangChain
60 LangGraph: Persistence
LangGraph: Persistence
LangChain

Create a self-healing code agent using LangChain and Open Evals to improve code generation accuracy and validate generated code through static analysis and reflection steps.

Key Takeaways
  1. Create a reflection step in your agent loop to validate generated code
  2. Leverage Open Evals for static analysis and type checking
  3. Use sandbox evaluation to check for runtime errors
  4. Integrate E2B for isolated sandbox environments
  5. Design effective prompts for code generation and reflection steps
💡 Adding a reflection step to your agent loop can significantly improve code generation accuracy and quality by catching type checking mistakes and validating generated code.

Related Reads

📰
GPT-5.5 Complete Guide in 2026
Learn about GPT-5.5, its features, performance, and why it matters in 2026, and how to leverage it for improved AI-assisted tasks
Dev.to AI
📰
AI Simplified — Why Structured Output Matters More Than a Fluent Answer
Learn why structured output is crucial for AI answers and how it can improve usability and automation
Dev.to AI
📰
I ran a 110B LLM on 16GB of RAM. Here's the equation that predicts any model's speed on your machine
Learn how to predict a large language model's speed on your machine using a simple equation, and discover how to run a 110B LLM on limited RAM
Dev.to · Federico Sciuca
📰
A Fidelity-First Workflow for Editing GPT-Generated Text
Learn a fidelity-first workflow for editing GPT-generated text to improve its quality and readability
Dev.to · Bisrat

Chapters (3)

Introduction
1:00 OpenEvals code evaluator introduction
3:32 Example implementation of reflection step in the loop - Mini Chat LangChain
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →