How to Create Streamlit UIs for AI Agents: A Production Pattern from Real-World Code
Build production-ready AI agent interfaces by implementing a six-layer Streamlit architecture that separates configuration management, input collection, agent execution, and result rendering, as demonstrated across dozens of reference implementations in the Arindam200/awesome-ai-apps repository.
The Arindam200/awesome-ai-apps repository showcases AI agents ranging from simple newsletter generators to complex multi-stage research workflows, all unified by a consistent Streamlit frontend pattern. Every implementation follows the same architectural layers—configuration, page setup, user input, agent invocation, result rendering, and error handling—making the UI code minimal, reusable, and easy to extend.
The Six-Layer Architecture for Streamlit AI Agents
1. Configuration Management
Securely load API keys and environment variables using python-dotenv. In simple_ai_agents/newsletter_agent/app.py, the pattern combines load_dotenv() with st.sidebar.text_input(..., type="password") to hide sensitive values while allowing runtime overrides.
from dotenv import load_dotenv
import streamlit as st
import os
load_dotenv()
api_key = st.sidebar.text_input(
"API Key", value=os.getenv("MY_API_KEY", ""), type="password"
)
2. Page Configuration
Establish consistent branding and layout using st.set_page_config(). This function sets the page title, favicon, and layout mode before any other UI elements render.
st.set_page_config(page_title="My AI Agent", page_icon="🤖", layout="wide")
3. User Input Collection
Gather execution parameters through Streamlit widgets. Common patterns include st.text_input for prompts, st.slider for numeric limits, st.selectbox for model selection, and st.file_uploader for RAG document ingestion.
prompt = st.text_input("Enter your query", placeholder="What do you want to know?")
max_results = st.slider("Results", min_value=1, max_value=10, value=5)
4. Agent Invocation
Instantiate the core agent—whether an Agno Agent, LangChain Chain, or custom Python class—within a st.spinner() context to provide visual feedback during processing. Asynchronous execution is wrapped in a button trigger to prevent premature runs.
if st.button("Run"):
with st.spinner("Thinking…"):
response = MyAgent(api_key=api_key).run(prompt, max_results)
5. Result Rendering
Display agent outputs using st.markdown() for formatted text and st.download_button() for file exports. This handles everything from simple text responses to generated markdown newsletters or research reports.
st.markdown(response)
st.download_button(
"Download", data=response, file_name="output.txt", mime="text/plain"
)
6. Error Handling
Surface exceptions gracefully using st.error(str(e)) to prevent UI crashes while providing actionable feedback to users when agent execution fails.
Production-Ready Implementation Template
The following skeleton appears across every Streamlit UI in the repository, from simple agents to complex workflows:
import streamlit as st
import os
from dotenv import load_dotenv
# 1️⃣ Load env vars (API keys, model endpoints)
load_dotenv()
api_key = st.sidebar.text_input(
"API Key", value=os.getenv("MY_API_KEY", ""), type="password"
)
# 2️⃣ Page configuration
st.set_page_config(page_title="My AI Agent", page_icon="🤖", layout="wide")
# 3️⃣ Input widgets
prompt = st.text_input("Enter your query", placeholder="What do you want to know?")
max_results = st.slider("Results", min_value=1, max_value=10, value=5)
# 4️⃣ Trigger execution
if st.button("Run"):
with st.spinner("Thinking…"):
# 5️⃣ Core agent call
response = MyAgent(api_key=api_key).run(prompt, max_results)
# 6️⃣ Show result
st.markdown(response)
st.download_button(
"Download", data=response, file_name="output.txt", mime="text/plain"
)
Real-World Examples in the Codebase
Newsletter Generator (simple_ai_agents/newsletter_agent/app.py)
This file demonstrates the complete six-layer pattern for a single-purpose agent. It collects topic inputs via st.text_input, search limits via st.slider, and renders generated markdown newsletters with st.markdown() and download functionality.
Video RAG System (rag_apps/video_rag/main.py)
Shows how to handle file uploads for video content using st.file_uploader(), integrate with vector stores for embedding generation, and present Q&A interfaces with streaming responses.
Memory-Enabled Agents (memory_agents/youtube_trend_agent/app.py)
Illustrates integration with external memory backends (such as memori) while maintaining the standard Streamlit UI pattern for input collection and result display.
Deep Researcher (advance_ai_agents/deep_researcher_agent/app.py)
Orchestrates multi-stage workflows—encompassing search, analysis, and writing stages—behind a unified Streamlit interface that presents simple inputs and aggregated outputs to users.
Agno Starter (starter_ai_agents/agno_starter/main.py)
The canonical minimal example showing how to wire an Agno Agent with Streamlit inputs and outputs, serving as the "Hello World" template for new agent development.
Summary
- Implement a six-layer architecture: Configuration, Page Setup, User Input, Agent Invocation, Result Rendering, and Error Handling
- Use
load_dotenv()with password-protected inputs to securely manage API keys across local and production environments - Wrap agent execution in
with st.spinner()to provide visual feedback during asynchronous processing - Reference
simple_ai_agents/newsletter_agent/app.pyfor single-purpose agent implementations with full UI feature coverage - Study
advance_ai_agents/deep_researcher_agent/app.pyfor complex multi-stage workflow orchestration patterns
Frequently Asked Questions
How do I securely manage API keys in a Streamlit AI agent?
Load environment variables at startup using python-dotenv, then provide password-protected input fields via st.sidebar.text_input(..., type="password") for local development overrides. As implemented in simple_ai_agents/newsletter_agent/app.py, this pattern ensures secrets are never hardcoded while remaining configurable across deployments.
What is the best way to handle long-running agent processes in Streamlit?
Wrap the agent execution code in a with st.spinner("Thinking..."): context manager to display a loading indicator while the agent processes asynchronously. Once complete, render the results using st.markdown() or st.json() depending on the output format.
Can Streamlit handle file uploads for RAG-based AI agents?
Yes. Use st.file_uploader() to accept documents or media files, then pass the uploaded file paths to your vector store or embedding pipeline. The rag_apps/video_rag/main.py implementation demonstrates video file handling with subsequent semantic search and Q&A generation.
How do I add download functionality for agent-generated content?
After generating content, call st.download_button() with the response data as bytes or string, specifying the appropriate file name and MIME type. This pattern is essential for document generators like the Newsletter Agent that produce markdown, PDF, or text files for user export.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →