For code generation agents, the key metrics are accuracy of generated code, BLEU score or similar text similarity metrics, and functional correctness. Accuracy here means how often the generated code matches the expected solution or passes tests. BLEU score measures how close the generated code is to reference code in wording and structure. Functional correctness is the most important because code must run correctly, not just look similar.
Code generation agent design in Agentic AI - Model Metrics & Evaluation
Start learning this pattern below
Jump into concepts and practice - no test required
Unlike classification, code generation does not use a confusion matrix. Instead, we can think in terms of pass/fail on test cases:
Test Cases: 100
Passed: 85
Failed: 15
Pass Rate = 85/100 = 85%
Fail Rate = 15/100 = 15%
This pass/fail count acts like a simple confusion matrix for code correctness.
In code generation, the tradeoff is between generating code that compiles and runs (precision) and covering all requested features or requirements (recall).
If the agent generates very safe but minimal code, it has high precision (few errors) but low recall (misses features). If it tries to generate complex code covering all features, it may have higher recall but lower precision due to bugs.
Example: A code agent that always generates a simple "Hello World" program has perfect precision but poor recall for complex tasks. One that tries to generate full apps may fail often, lowering precision.
Good: Pass rate above 90%, BLEU score above 0.7, and generated code passes all functional tests.
Bad: Pass rate below 50%, BLEU score below 0.3, and generated code frequently fails to compile or run.
Good code generation means the agent reliably produces working code that meets requirements. Bad means frequent errors or incomplete solutions.
- Overfitting: Agent memorizes training code but fails on new tasks.
- Data Leakage: Test code appears in training data, inflating pass rates.
- Accuracy Paradox: High BLEU score but generated code does not run correctly.
- Ignoring Functional Tests: Relying only on text similarity without running code.
Your code generation agent has 98% accuracy on training data but only 12% pass rate on new test cases. Is it good for production? Why or why not?
Answer: No, it is not good. The high training accuracy suggests overfitting, meaning the agent memorized training code but cannot generate correct new code. The low pass rate on test cases shows poor generalization, which is critical for production use.
Practice
What is the main purpose of a code generation agent in AI?
Solution
Step 1: Understand the role of a code generation agent
A code generation agent is designed to write code automatically based on instructions it receives.Step 2: Compare options with this role
Only To automatically write code from given instructions matches this purpose. Other options describe unrelated tasks.Final Answer:
To automatically write code from given instructions -> Option CQuick Check:
Code generation agent purpose = automatic code writing [OK]
- Confusing code generation with debugging
- Thinking it executes code faster
- Assuming it replaces all programmers
Which of the following is the correct way to instruct a code generation agent to create a Python function named add that returns the sum of two numbers?
Solution
Step 1: Identify the correct instruction for addition
The instruction must specify a function named add that returns the sum (x + y).Step 2: Check each option
Write a function add(x, y) that returns x + ycorrectly instructs to write a function add(x, y) returning x + y. Others describe subtraction or multiplication.Final Answer:
Write a function add(x, y) that returns x + y -> Option DQuick Check:
Correct function instruction =Write a function add(x, y) that returns x + y[OK]
- Choosing instructions for subtraction or multiplication
- Ignoring function name or return statement
- Confusing wording of instructions
Given this instruction to a code generation agent: Write a Python function multiply that returns the product of two numbers. Which of the following code outputs is correct when calling multiply(3, 4)?
Solution
Step 1: Understand the function's purpose
The function multiply should return the product of two numbers, so multiply(3, 4) should return 3 * 4 = 12.Step 2: Evaluate each output option
12 is 12, which matches the expected product. Others are incorrect or errors.Final Answer:
12 -> Option BQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Assuming function causes error
Consider this code generated by an agent:
def divide(x, y):
return x / y
result = divide(10, 0)What is the main issue with this code?
Solution
Step 1: Analyze the function call
The function divide is called with y=0, which causes division by zero.Step 2: Identify the error type
Division by zero causes a runtime error (ZeroDivisionError) in Python.Final Answer:
Runtime error due to division by zero -> Option AQuick Check:
Divide by zero causes runtime error [OK]
- Thinking it's a syntax error
- Assuming code runs without error
- Confusing logical error with runtime error
You want a code generation agent to create a Python function that filters out all negative numbers from a list and returns the positive numbers only. Which instruction will most likely produce the correct function?
Solution
Step 1: Understand the filtering goal
The goal is to keep only positive numbers, so the instruction must specify returning positive numbers.Step 2: Evaluate each instruction
Write a function that returns only positive numbers from the listcorrectly asks for a function returning only positive numbers. Others do different tasks.Final Answer:
Write a function that returns only positive numbers from the list -> Option AQuick Check:
Filter positive numbers =Write a function that returns only positive numbers from the list[OK]
- Choosing instructions that filter negatives instead
- Confusing filtering with summing or sorting
- Ignoring the word 'positive' in instruction
