Legacy LLM Layer
Overview
The legacy LLM layer under pentestgpt_legacy/llm/ is the compatibility boundary between the original synchronous PentestGPT reasoning core and modern per-provider async SDKs. Its public surface is exported from pentestgpt_legacy/…/__init__.py: get_client(), list_models(), MODELS, PROVIDERS, and the LLMClient wrapper.
pentestgpt_legacy/…/registry.py is the source of truth for provider and model selection. ProviderInfo records each provider key, display label, connector kind (openai, anthropic, or gemini), accepted API-key env vars, default base URL, and whether credentials are required. ModelSpec maps the user-facing model id to the provider and wire api_id, context window, tier, aliases, legacy/reasoning metadata, token-cap parameter name, and whether OpenAI must use the Responses API. The registry includes native OpenAI, Anthropic, and Gemini providers plus OpenAI-compatible DeepSeek, xAI, Qwen, Moonshot, and local Ollama endpoints. resolve() handles canonical ids, aliases, and dynamic ollama:<model> ids by creating an Ollama ModelSpec with the local model name as api_id; models_by_provider() powers grouped CLI display; DEFAULT_REASONING_PREFERENCE and DEFAULT_PARSING_PREFERENCE drive app-level default model choice.
Credentials and endpoint overrides are centralized in pentestgpt_legacy/…/config.py. LLMSettings loads .env and environment variables through pydantic-settings, accepts alternate env names for Gemini (GOOGLE_API_KEY), xAI (XAI_API_KEY), Qwen (DASHSCOPE_API_KEY), and Moonshot (MOONSHOT_API_KEY), and exposes api_key_for() / base_url_for() for a ProviderInfo. The singleton get_settings() means environment and .env are read once per process. configured_providers() returns providers with a key, plus no-key providers such as Ollama.
pentestgpt_legacy/…/factory.py is the construction path. get_client(model_name, system_prompt=None, history_length=40) resolves the model id, looks up the provider in PROVIDERS, builds the appropriate connector class from _PROVIDER_CLASSES, enforces required credentials with MissingCredentialsError, and returns an LLMClient. Unknown ids raise UnknownModelError with all registry ids and the ollama:<model> hint. list_models() returns the registry in display order.
pentestgpt_legacy/…/base.py defines the provider contract: an async BaseProvider.acomplete(messages, system, spec, max_output_tokens=None, temperature=None) -> str, where messages are only user/assistant turns and provider implementations decide how to transmit the system prompt. pentestgpt_legacy/…/client.py adapts that async interface to the synchronous API expected by the classic core. LLMClient.send_new_message() creates a UUID conversation id, send_message() continues or creates a conversation slot, and _chat() appends the user message, trims history, calls provider.acomplete() through run_sync(), then appends the assistant response. _trim() keeps the most recent history_length messages, also trimming against roughly 80% of the model context window using a 4-chars-per-token budget and dropping leading assistant turns so Anthropic receives a user-starting sequence. run_sync() uses asyncio.run() normally and switches to a dedicated thread when called from an already-running event loop.
Provider implementations live in pentestgpt_legacy/llm/providers/. openai_compatible.py uses AsyncOpenAI for OpenAI and any OpenAI-compatible provider by passing the provider base URL. It sends a non-empty placeholder key for Ollama, routes spec.responses_api models through client.responses.create() with instructions and input, and otherwise uses client.chat.completions.create() with an optional prepended system message. Temperature is only sent when explicitly provided, and output caps use spec.max_tokens_param for chat models but max_output_tokens for Responses API models. anthropic_provider.py uses AsyncAnthropic.messages.create(), passes the system prompt as Anthropic's top-level system, always supplies max_tokens (defaulting to 8192 when no cap is passed), and reconstructs the reply by concatenating text blocks. gemini_provider.py uses google-genai, maps user messages to Gemini user and all non-user messages to model, passes the system instruction and generation options through GenerateContentConfig, and disables major safety categories because security-testing content often trips default Gemini filters.
The legacy CLI and orchestrator consume this layer directly. pentestgpt_legacy/config.py imports configured_providers() and the registry default preference tuples, then chooses the first preferred model whose provider is configured. pentestgpt_legacy/main.py exposes --reasoning-model, --parsing-model, --generation-model, --base-url, --list-models, and --smoke-test; display_models() renders models_by_provider(), and _apply_base_url_override() writes <PROVIDER>_BASE_URL env vars before clients are constructed. pentestgpt_legacy/…/pentest_gpt.py creates three LLMClient sessions via get_client(): reasoning maintains the Pentesting Task Tree, generation expands tasks into guidance, and parsing summarizes long tool or web output. It initializes each session with a prompt using send_new_message() and then routes updates through send_message() with the saved conversation ids. pentestgpt_legacy/smoke_test.py bypasses the synchronous conversation wrapper after construction and calls client.provider.acomplete() for each registry model whose provider is configured.
Coupling
- If pentestgpt_legacy/…/registry.py adds or renames a provider key, also update pentestgpt_legacy/…/config.py
_KEY_FIELDand any base-url field expected byLLMSettings.base_url_for().factory._build_provider()trustsProviderInfo.keyto line up with those settings fields, so a mismatch can make a configured provider look credentialless or ignore endpoint overrides. - If
ProviderInfo.kindgains a new connector kind in pentestgpt_legacy/…/registry.py, also update_PROVIDER_CLASSESin pentestgpt_legacy/…/factory.py and export the class from pentestgpt_legacy/…/__init__.py; otherwiseget_client()will fail with a key lookup after model resolution succeeds. - If
ModelSpecfields that affect wire format change in pentestgpt_legacy/…/registry.py (api_id,max_tokens_param,responses_api, orreasoning), verify pentestgpt_legacy/…/openai_compatible.py. It uses those fields to choose Chat Completions vs Responses API and to decide the token-cap parameter sent to the SDK. - If the
LLMClient.send_new_message()/send_message()signatures or conversation-id behavior in pentestgpt_legacy/…/client.py change, update pentestgpt_legacy/…/pentest_gpt.py. The orchestrator stores three session ids and assumes conversation state is held inside each client rather than remotely. - If CLI
--base-urlbehavior changes in pentestgpt_legacy/main.py, verify pentestgpt_legacy/…/config.py andfactory.get_client(): overrides are currently implemented by setting provider-specific env vars before the singleton settings object is first created.
Risks
- pentestgpt_legacy/…/config.py caches
LLMSettingsin_settings, while pentestgpt_legacy/main.py applies--base-urlby mutatingos.environ. If any code callsget_settings()before_apply_base_url_override(), the override will not be observed and clients may connect to the registry default endpoint. - pentestgpt_legacy/…/client.py accepts
image_urlparameters onsend_new_message()andsend_message()for legacy compatibility but ignores them. If callers assume screenshot or image context reaches a provider, the model will receive only the text message. - pentestgpt_legacy/…/gemini_provider.py maps every non-user role to Gemini
model. If future callers introduce roles beyonduserandassistant, they will be sent as model turns rather than rejected or transformed explicitly.