Agents in Production: Build an AI Chat App with Shiny and {shinychat} Jumping Rivers published part two of its five-part "Agents in Production" series, showing how to wrap an R-based clinical trial safety agent in a Shiny web app using Posit's {shinychat} package. The post uses {shinychat} 0.5.0's chat_ui() and chat_server() components, which soft-deprecate the earlier chat_mod_ui() and chat_mod_server(), to give each user a separate conversation, display the agent's tool calls inline, block file uploads with allow_attachments = FALSE, and handle slow or unavailable models before deploying to Posit Connect. The tutorial also sets options(shiny.sanitize.errors = TRUE) so a failed answer shows "An error occurred" rather than the provider's raw error message. Agents in Production: Build an AI Chat App with Shiny and {shinychat} , and kindly contributed to The Jumping Rivers Blog https://www.jumpingrivers.com/blog/agents-in-production-shiny/ R-bloggers https://www.r-bloggers.com/ . You can report issue about the content on this page here https://www.r-bloggers.com/contact-us/ Want to share your content on R-bloggers? click here https://www.r-bloggers.com/add-your-blog/ if you have a blog, or here http://r-posts.com/ if you don't. The agent we built in part 1 https://www.jumpingrivers.com/blog/agents-in-production-first-agent/ lives in an R console, which suits one person. A trial team needs an app in the browser, where several people can ask questions at once without seeing each other’s conversations. This is part two of a five-part series on putting AI agents into production with R. Each part builds on the same app, a clinical trial safety assistant. The parts are: - From Chatbot to Agent https://www.jumpingrivers.com/blog/agents-in-production-first-agent/ - Putting the Agent in a Shiny App https://www.jumpingrivers.com/blog/agents-in-production-shiny/ this post - Can You Trust It? - Audit Trails, Costs and Models - Sharing Your Tools with MCP As a reminder, the agent answers questions about a clinical trial’s safety data with a model, four tools and a loop. The model chooses which tool to call, and the tools do the counting in R. The model is also told never to do the maths itself, and part 3 checks that. Part 1 also explains the trial terms https://www.jumpingrivers.com/blog/agents-in-production-first-agent/ trial-terms-in-plain-english . In this post we: - build a chat app around the agent with {shiny} and {shinychat} - give every user their own conversation, and test it - show the agent’s tool calls in the chat - stop files being uploaded to the model - handle the model being slow or unavailable, and deploy to Posit Connect What is {shinychat}? {shinychat} https://posit-dev.github.io/shinychat/ is Posit’s package of chat components for Shiny, and we need two of them. The chat window comes from chat ui . Behind it, chat server connects the window to an {ellmer} chat client. It sends each question to the model, streams the answer back and shows each tool call as it happens. The client is the one part 1’s make agent builds, so the tools, the system prompt and the loop all carry over unchanged. If you’ve used chat mod ui and chat mod server before, {shinychat} 0.5.0 soft-deprecates them in favour of these two. The app We keep the page in its own function, app ui , so that tests can check it without starting the app: GREETING <- paste "Ask me about adverse events in CDISC pilot study 01: a xanomeline skin patch at two doses, against placebo.", "For example: Which adverse events were most common on the high dose? ", "I only know this trial's safety data, and I can't give medical advice.", sep = "\n\n" app ui <- function { bslib::page fillable title = "Trial safety assistant", shinychat::chat ui "chat", greeting = GREETING, placeholder = "Ask about the trial's adverse events", allow attachments = FALSE } The greeting tells people what the assistant is for. It only sets expectations, though. The rule against medical advice goes into the system prompt in part 3, where we also test that the agent keeps it. The app itself is short, because the work happens in the files in R/ : part 1’s, plus four new ones ui.R , display.R , wrap.R and attachments.R , which Shiny sources before app.R runs: library shiny library bslib library shinychat shinychat writes a failed answer's error into the chat. Sanitised, it says "An error occurred" instead of showing the provider's raw message. options shiny.sanitize.errors = TRUE Shiny sources every file in R/ before this runs. trial data <- load trial data ui <- app ui server <- function input, output, session { One client per session. A client made outside server would share one conversation between every user. client <- make agent tools = with display trial tools trial data refuse attachments client chat <- chat server "chat", client, history = FALSE observeEvent chat$last error , { error <- chat$last error message <- if inherits error, "trial attachment refused" { conditionMessage error } else { "The assistant couldn't answer just now. Please try again in a minute." } showNotification message, type = "error", duration = NULL } } shinyApp ui, server For each person who opens the app, server does four things: - It makes them their own chat client. make agent builds it with the four tools, each wrapped by with display so that its results read well in the chat. - It checks every request before it goes to the model , with refuse attachments . - It connects the client to the chat window. That’s chat server , with its built-in conversation history turned off history = FALSE . Left on, {shinychat} 0.5.0 saves every conversation as files on the server, patient records included, outside the audit log and any retention rule, and asks the model to title each one. Part 4 keeps one record instead. - It tells the user what to do if a question fails , by watching chat$last error . The data is loaded once, outside server , and shared by every session. That’s safe because nothing changes it. To run the app, get this part’s folder as in part 1 and open it as a project. Your key doesn’t come with it, since git ignores .Renviron , so copy that file across from part 1’s folder. Then run renv::restore , which is quick because {renv} reuses the packages it installed before, or install the new ones with install.packages c "shiny", "shinychat", "rsconnect" . Finally, call shiny::runApp . One conversation per user An {ellmer} chat client sends the conversation so far with each new question. That’s what makes follow-ups work, and it means a client belongs to one person. Shiny runs one R process for many users, and it calls server once for each browser session. Anything created inside server belongs to that session, while anything created at the top of app.R is shared by everyone. So a client created at the top of the file puts every user in the same conversation. The second person to arrive can ask “what did I just ask?” and get the first person’s question back. You won’t catch this by trying the app on your own, so we test for it. Shiny’s testServer runs the server function without a browser. Our test builds one app with shiny::shinyAppDir , runs testServer on it twice, and checks that the two sessions never share a chat$client . For the browser tests that go alongside testServer , see our series on {shinytest2} https://www.jumpingrivers.com/blog/end-to-end-testing-shinytest2-part-1/ . test that "two sessions never share a client", { withr::local envvar GEMINI API KEY = "test-key" One app, as on a server: both sessions run inside it. Given a folder, testServer builds a fresh app each time, which would hide a shared client. app <- shiny::shinyAppDir test path "..", ".." seen <- new.env for i in 1:2 { shiny::testServer app, assign paste0 "client", i , chat$client, envir = seen } expect false identical seen$client1, seen$client2 } Showing the work {shinychat} shows the tool calls as rows above the answer. Each row takes its title from the tool annotations title = … we gave the tool in part 1, such as “Counting subjects” or “Summarising adverse events”. Opening a row shows what the agent asked for and what came back, so a trial team can check where a number came from. By default, calls to the same tool share one row, which lists each call when you open it. Set tool grouping = "none" in chat ui to give every call its own row. The raw results are JSON, which suits the model but is hard for people to read. So with display wraps each tool, and people see a readable table while the model still gets JSON: with display <- function tools { lapply tools, function tool { wrap tool tool, function run, args { value <- run The display is for people. If it can't be built, the model still gets its answer. markdown <- tryCatch result markdown value , error = function e NULL if is.null markdown return value ellmer::ContentToolResult value = value, extra = list display = shinychat::tool result display markdown = markdown } } } Two helpers do the work. The first, wrap tool in R/wrap.R , copies a tool so that every call goes through a function we give it. Inside that function, run calls the original tool and args holds the arguments the tool will run with, including any defaults the model left out. The second, result markdown in R/display.R , turns the JSON into a table or a list. We use wrap tool again in part 4, for the audit log. Not every tool returns JSON. The handover tool in part 3 returns a plain sentence, and our first version of the display broke on it, taking the whole tool call down too. It now falls back to plain text. No file uploads By default, chat server allows file uploads. People can attach a CSV file, a PDF or an image, and it goes straight to the model. That undoes part 1’s design. The model would read raw data itself instead of asking our tested tools, and the whole file would go to the provider, when a question only needs a few numbers. Setting allow attachments = FALSE in chat ui only hides the upload button, and anyone can send a request without using the page. So the server checks every request as well. It refuses any user turn that holds more than typed text and tool results {ellmer} sends tool results back as user turns . Text files need one more check. {shinychat} sends a CSV or TXT file as plain text in a