Unified Integration with Replaceable Icons for Multi-Model Large Language Models

    This article discusses a maintainable approach to integrating multiple large language models in chat applications, with a focus on replaceable icons and decoupled model management. It breaks down the problem into presentation, capability, integration, and runtime layers, and proposes a model profile abstraction with adapter-based connections. The article also covers icon resource organization, switching and fallback strategies, security considerations, and testing guidelines to ensure stable multi-model support and easy maintenance.

    Background and Problem

    Integrating multiple large language models into a single chat application has become common practice. Different model providers differ in specialized tasks, pricing, and regional availability. Users typically need to see which model is currently active in the UI, and the model icon is the most direct indicator. However, if icon rendering and model logic are tightly coupled, every new model addition or replacement requires UI code changes, leading to rising maintenance costs.

    The core requirement is separation of concerns. The UI only cares about display information, while the business layer cares about calling capabilities. These are connected through a unified model description.

    Problem Breakdown

    The problem can be divided into four layers:

    1. Presentation layer: icon, name, color, ordering, and short description.
    2. Capability layer: streaming support, context length limit, image input, etc.
    3. Integration layer: request URL, authentication method, request body format.
    4. Runtime layer: timeout, retry count, rate limiting, and fallback order.

    Each layer changes at different frequencies. The presentation layer changes least often, while the integration layer changes more frequently. Layering allows one layer to be replaced without affecting others. For example, changing a model’s icon only requires updating the resource table, not the integration code.

    Unified Abstraction: Model Profile and Adapter

    It is recommended to define a profile object for each model, containing an internal unique identifier, display name, icon resource key, capability tags, and a reference to integration configuration. Implementation details are handled by adapters, each associated with a provider and exposing the same interface.

    1. from dataclasses import dataclass, field
    2. @dataclass
    3. class ModelProfile:
    4. model_id: str # Internal unique identifier, stable across provider renames
    5. display_name: str # Name shown in the UI
    6. icon_key: str # Icon resource key, resolved by the resource table to a concrete file
    7. capabilities: set = field(default_factory=set)
    8. provider: str = "" # Reference to integration config; secrets are not stored here
    9. class ChatAdapter:
    10. """Each model provider implements an adapter with a consistent interface."""
    11. def send(self, messages: list[dict], timeout: float = 15.0) -> str:
    12. raise NotImplementedError

    The internal unique identifier model_id is crucial. Historical conversation records should store this identifier rather than the provider name. This way, even if the underlying integration changes, the association with previous records remains clear.

    Icon Resource Organization

    Icon management should follow these conventions:

    • Resource files are named after icon_key, using generic IDs or functional descriptions, e.g., icon-general-01, avoiding any third-party brand names.
    • Mappings are stored in a separate resource table; UI code resolves icons by key, not by direct file path.
    • A default icon is prepared for each application. When mapping is missing, the UI shows the default icon instead of an error or blank space.
    • Icon sizes are uniform, preferably vector or multi-resolution formats, and support light and dark themes.
    • Third-party brand logos are often protected by trademarks. Before importing external icons, confirm usage authorization, or use neutral custom graphics instead.

    Icon replacement only modifies the resource table; no code changes are required. This also facilitates gradual rollout, such as switching icons for a subset of users first and observing feedback before full deployment.

    Switching and Fallback Strategies

    Model switching involves three scenarios: manual switching, automatic fallback, and capability checks.

    1. Manual switching: When a user switches models, the current conversation is preserved but each reply records the model_id that generated it, preventing misattribution of historical messages.
    2. Automatic fallback: If the primary model times out or returns an error, backup models are attempted according to a pre-configured order. Every fallback should show a UI hint so users know which model produced the reply.
    3. Capability check: If the current conversation contains images and the target model does not support image input, the user should be warned before switching, rather than failing silently when sending.

    Fallback logic example:

    1. def ask_with_fallback(candidates, messages, registry):
    2. """Try candidate models in order; return the first successful result and its profile."""
    3. last_error = None
    4. for profile in candidates:
    5. adapter = registry.get_adapter(profile.model_id)
    6. try:
    7. reply = adapter.send(messages, timeout=15.0)
    8. return profile, reply
    9. except TimeoutError as exc:
    10. last_error = exc
    11. raise RuntimeError("All candidate models failed") from last_error

    The fallback chain should be limited in length. Too many retries amplify request volume and lengthen user wait times. Usually two to three candidate models are sufficient for most failure scenarios.

    Configuration and Security

    Model integration involves credentials and user data, so the following requirements must be met:

    • Secrets are stored in environment variables or a dedicated secret management service. Configuration files contain only reference names, not plaintext.
    • Each adapter uses the least-privilege credentials, only exposing the interface scope needed for calls.
    • Logs do not include full request and response content because conversations may contain personal information. Logs may include model identifiers, latency, and error categories.
    • Provider raw error messages are not shown directly to users. They should be translated into unified error categories, such as timeout, insufficient quota, content rejected, etc., while the server-side retains the original details for troubleshooting.

    Testing Considerations

    After completing the design, the following tests are recommended:

    • Contract tests: Send the same test message to each adapter and verify the structure and type of return values are consistent.
    • Mapping tests: Iterate through the model registry to confirm every icon_key resolves to a resource file or a default icon.
    • Fallback tests: Simulate timeouts and error responses, verifying candidate order, hint text, and final return values.
    • Compatibility tests: After a model is retired, old conversations should still display properly, and model identifiers and icons on messages should fall back to default display correctly.

    Conclusion

    By separating the presentation, capability, integration, and runtime layers, and connecting them with an internal unique identifier, icon replacement becomes a simple update to the resource table. This structure makes it easy to add new models and keeps UI and business risks under control during model switching.