AI Made Friendly HERE

A Hybrid Approach Combining Prompt Engineering and Static Analysis|開発の実験室

Introduction

With the advancement of generative AI, code generation using tools like ChatGPT and Claude has become a routine part of modern development environments. In particular, workflows where AI agents autonomously accomplish tasks are dramatically boosting development efficiency. However, there remains an ongoing risk that the code generated by agents will be plagued by “hallucinations,” with no end to cases where nonexistent library methods are invoked or implementations contradict type definitions.

In this article, we explain a hybrid verification method combining prompt engineering and static analysis tools to enhance the reliability of code generation by AI agents.

The Mechanism Behind AI Agent “Hallucinations”

While AI agents generate code that is probabilistically plausible, they do not necessarily have an accurate grasp of complex, project-specific dependencies or the latest library specifications. Hallucinations are especially prone to occur in cases such as the following:

  • Use of deprecated, legacy APIs
  • Reliance on nonexistent type definitions
  • Misinterpretation of boundary conditions in complex business logic

To prevent these issues, rather than relying entirely on AI output, it is necessary to incorporate “mechanical verification” as part of the CI/CD pipeline.

Implementation Approach for Hybrid Verification

To ensure reliability, we build the following three-stage verification flow:

1. Prompt Optimization via Context Injection

When having AI generate code, instead of simply asking it to “implement this,” inject relevant type definition files (.d.ts) and interface definitions as context. This significantly reduces the likelihood of the AI attempting to access “nonexistent properties.” A technique using RAG (Retrieval-Augmented Generation) to dynamically retrieve related code within the repository and pass it as a system prompt is effective.

2. Automated Inspection via Static Analysis Tools

Rather than committing the AI-generated code directly to the repository, place it in a temporary directory and run the TypeScript compiler (tsc) or ESLint. If errors occur at this stage, build a loop that feeds the error messages back to the AI to prompt corrections.

#!/bin/bash
# エラーメッセージをAIに再送するループの例
MAX_RETRIES=3
COUNT=0

while [ $COUNT -lt $MAX_RETRIES ]; do
if tsc –noEmit –project tsconfig.json; then
echo “静的解析をパスしました。”
exit 0
else
echo “解析エラー発生。AIにフィードバック送信中…”
# ここでエラーログをAI APIへ送信する処理を呼び出す
((COUNT++))
fi
done
echo “自動修正の試行回数上限に達しました。”
exit 1

3. Verification via Test Code

Even if the code passes static analysis, that does not guarantee the logic is correct. Have the AI agent generate test code (such as Jest or Vitest) concurrently with the implementation, and confirm that those tests pass. When a test fails, modern architecture incorporates a “self-correction process” that has the agent itself perform debugging. Feeding coverage reports back to the AI can also prompt it to fill in untested areas.

Integration into Development Workflows and Security

When integrating this approach into production workflows, please keep the following points in mind:

  • Setting Guardrails: Minimize security risks by restricting the commands the AI agent can execute (e.g., prohibiting rm -rf or unrestricted access to external networks) and running it in a sandboxed environment.
  • Final Human Review: Automation is merely an aid; humans must always conduct code reviews for changes related to critical business logic and security.
  • Secrets Management: Thoroughly manage environment variables to ensure that environment variables and API keys are not included in prompts.

Conclusion

Although AI agents are powerful tools, treating their output as “suggestions” and filtering them through “objective evaluation criteria” such as static analysis and testing is key to robust software development. We recommend starting by introducing this verification flow to small-scale refactoring tasks and gradually expanding the scope of automation.

Maintaining a stance of believing in AI’s potential while never neglecting mechanical verification can be considered the essential skill set required for engineers in the AI era. Static analysis and test automation serve not merely as bug prevention measures, but as a “common language” to accelerate collaboration with AI.

Related Articles

Preventing “Thought Loops” in Autonomous Task Execution by AI Agents: ReAct Patterns and Safe Guardrail Design

Originally Appeared Here

You May Also Like

About the Author:

Early Bird