AI Security
Study: External Security Evidence Can Cut Java API Misuse in AI-Generated Code by Up to 32%
Researchers tested how retrieval-augmented prompting affects LLMs' ability to correctly implement cryptographic functions, finding that retrieving relevant documentation substantially reduces security vulnerabilities.
Large language models can produce code that compiles and runs but still contains serious security flaws. A new study from arXiv examines how providing LLMs with relevant security documentation and code examples during generation affects their tendency to misuse Java cryptography APIs.
The Problem: Hidden Vulnerabilities in Valid Code#
Researchers evaluated GPT-5.5 and Llama-3.3-70B-Instruct on tasks involving Java Cryptography Architecture (JCA) and Java Secure Socket Extension (JSSE) functions. Without any external evidence or guidance, the results were alarming: nearly 59% of valid Llama-generated programs and 37% of valid GPT-5.5 programs contained detectable security API misuse—despite being syntactically correct and executable.
How External Evidence Helps#
The team tested whether providing retrieved security documentation and code examples during generation could reduce these vulnerabilities. When dense retrieval methods fed the models relevant evidence from a full corpus of security materials, conditional misuse dropped significantly:
- Llama-3.3-70B-Instruct: 29–33 percentage point reduction
- GPT-5.5: 22–31 percentage point reduction
This demonstrated that reference material substantially improves security outcomes, though the effect varies by model and retrieval strategy.
Different Retrieval Methods, Different Outcomes#
The study compared four different retrieval approaches, revealing distinct preferences:
- BM25 and OpenAI embeddings: Prioritized official guidance documents
- Qwen embeddings: Favored code examples
- Jina embeddings: Selected a more mixed set of evidence types
When researchers restricted retrieval to single sources, code examples proved most effective for Llama, while explicit misuse patterns (showing what not to do) achieved zero detected misuse for GPT-5.5 on known test cases—though this came at a cost to code validity (94.1%).

