Why assistant-written code leaks keys
A model asked to make an example work will produce a working example, and a working example usually needs a credential. It writes one in the obvious place because the prompt asked for runnable code rather than deployable code.
The second route is subtler: a developer pastes a failing snippet including the real key, asks why the connection fails, and pastes the answer back. The secret enters the file as part of the fix, disguised as the thing that was already there.
A third route is documentation. Example blocks, README quickstarts and test fixtures are still source, they still get committed, and they are read by people who copy them.