AutoGenLib is a Python library that integrates directly into the language's import system. When you attempt to import a module or function that does not yet exist, the library calls the OpenAI API to generate the necessary code on the fly based on a high-level description.
Dynamic code generation – Import modules and functions that haven't been written yet.
Context-aware – When generating new functions, AutoGenLib examines your existing code to maintain consistency across the project.
Progressive enhancement – Add new features to existing modules seamlessly without breaking current functionality.
No default caching – By default, every import triggers a fresh generation. This results in variety and, occasionally, unexpected architectural approaches.
Full codebase context – The LLM has access to all previously generated modules, ensuring that new code remains compatible with what was created earlier.
Caller code analysis – The library analyzes the specific code block attempting the import. This provides the LLM with specific clues about the intended use case.
Automatic error handling – Runtime errors are passed back to the LLM, which then explains the issue and suggests potential fixes.
Install the library via pip:
pip install autogenlib
Or install from the source repository:
git clone https://github.com/cofob/autogenlib.git
cd autogenlib
pip install -e .
First, set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-api-key-here"
The following example demonstrates the library in action. Although the function generate_token does not exist in the codebase, AutoGenLib creates it during the import process:
from autogenlib.tokens import generate_token
token = generate_token(length=32)
print(token)
autogenlib namespace:Generate a TOTP generator
from autogenlib.totp import totp_generator
print(totp_generator("SECRETKEY123"))
Add a verification function to the same module later
from autogenlib.totp import verify_totp
result = verify_totp("SECRETKEY123", "123456")
print(f"Verification result: {result}")
Context awareness in action
from autogenlib.processor import get_highest_score
data = [{"user": "Alice", "score": 95}, {"user": "Bob", "score": 82}]
print(get_highest_score(data)) # The LLM correctly identifies how to extract the maximum score from the dictionary list.
Creating multiple modules
from autogenlib import init
init("Password utilities library")
# Generate an encryption module
from autogenlib.encryption import encrypt_text, decrypt_text
encrypted = encrypt_text("secret message", "password123")
decrypted = decrypt_text(encrypted, "password123")
print(decrypted)
# Generate a hashing module
from autogenlib.hashing import hash_password, verify_password
hashed = hash_password("my_secure_password")
is_valid = verify_password("my_secure_password", hashed)
print(f"Password valid: {is_valid}")
Setting the OpenAI API key – Use an environment variable for security:
export OPENAI_API_KEY="your-api-key-here"
# Optional settings:
export OPENAI_API_BASE_URL="https://openrouter.ai/api/v1"
export OPENAI_MODEL="openai/gpt-4.1"
You can also set these variables directly in Python (though this is not recommended for production environments):
import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"
Caching – By default, AutoGenLib does not cache generated code. Every import triggers a new LLM request, which allows for creative variations but may result in different implementations across different runs.
To enable caching for consistency or to reduce API costs:
from autogenlib import init
init("Data processing library", enable_caching=True)
You can also toggle caching at runtime:
from autogenlib import init, set_caching
init("Data processing library")
# Later in your script:
set_caching(True) # Enable caching
set_caching(False) # Disable caching
When caching is enabled, generated code is stored locally in ~/.autogenlib_cache.
Viewing generated code
You can use standard Python tools to inspect the code that AutoGenLib produces for a specific module:
from autogenlib.totp import totp_generator
import inspect
print(inspect.getsource(totp_generator))
How AutoGenLib builds the OpenAI prompt
To ensure high-quality output, the prompt sent to the LLM includes:
LightlyStudio: Reduce Annotation Costs Through Intelligent Data Curation
Prompt Tools: Open-Source Desktop App to Stop Losing Your Best AI Prompts
VibeVoice: Long-Form Multi-Speaker TTS for Natural Dialogue Generation
Eigent: Multi-Agent Workflow Desktop App with CAMEL and MCP
Cline AI Coding Assistant for VS Code: Powered by Claude Sonnet
Ccundo: Smart Undo and Redo Tool for Claude Code Sessions
Transformers Library: Installation, Pipeline API, and Model Examples
MindForger Review: A Private Markdown IDE for Personal Knowledge Management
SerenityOS Build Guide: A C++ Unix-Like System for x86, Arm, and RISC-V
Mevzuat MCP: Search Turkish Legislation Directly in Claude
ConEmu: A Highly Customizable Windows Terminal with Tabs and Split Panes
DBeaver: A Free Cross-Platform Database Tool (Plus CloudBeaver)