Build a provider-neutral chatbot
This minimal terminal chat keeps message history in the current process and can switch providers without changing the conversation loop.
Install and configure
python -m pip install llmswap
export OPENAI_API_KEY="your-key"
Create chatbot.py
from llmswap import LLMClient, LLMSwapError
def main():
client = LLMClient(provider="openai", model="gpt-5.6")
messages = []
print("Type /quit to exit")
while True:
prompt = input("You: ").strip()
if not prompt:
continue
if prompt == "/quit":
break
messages.append({"role": "user", "content": prompt})
try:
response = client.chat(messages, cache_bypass=True)
except LLMSwapError as error:
print(f"Error: {error}")
messages.pop()
continue
print(f"Assistant: {response.content}")
messages.append({"role": "assistant", "content": response.content})
if __name__ == "__main__":
main()
Run it:
python chatbot.py
Change providers
Change only client construction:
client = LLMClient(provider="anthropic", model="claude-sonnet-5")
Or auto-detect the first configured provider:
client = LLMClient()
Production considerations
- Keep API keys in an environment or secret manager, not source code.
- Set
fallback=Falseif data must never move to another configured provider. - Bound conversation history according to the selected model’s context window.
- Validate model output before it triggers external actions.
- Add application-level authentication, rate limits, timeouts, and observability.
- Review provider retention and data-use terms for the account being used.
For tool calling, async access, and Best Answer, see the Python SDK guide.