Langchain and Langsmith
Deploy an executive assistant using Langsmith and Langchain
In this tutorial, we’ll create Cal-vin, an executive assistant that manages calendar appointments (via Cal.com) with employees, customers, partners, and friends. Using the LangChain SDK for agent creation and LangSmith platform for monitoring, we’ll track scheduling activities and identify failure points. Finally, we’ll deploy on Cerebrium to demonstrate seamless deployment and scaling.
You can find the final version of the code here
Concepts
This app requires calendar interaction based on user instructions - an ideal use case for an agent with function (tool) calling capabilities. LangChain, a framework with extensive agent support, created LangSmith, making integration straightforward.
A tool refers to any framework, utility, or system with defined functionality for specific use cases, such as searching Google or retrieving credit card transactions.
Key LangChain concepts:
ChatModel.bind_tools()
: Attaches tool definitions to model calls. While providers have different tool definition formats, LangChain provides a standard interface for versatility. Accepts tool definitions as dictionaries, Pydantic classes, LangChain tools, or functions, telling the LLM how to use each tool.
AIMessage.tool_calls
: An attribute on AIMessage that provides easy access to model-initiated tool calls, specifying invocations in the bind_tools format:
create_tool_calling_agent()
: Unifies the above concepts to work across different provider formats, enabling easy model switching.
Setup Cal.com
Cal.com provides our calendar management foundation. Create an account here if needed. Cal serves as the source of truth - any updates to time zones or working hours automatically reflect in the assistant’s responses.
After creating your account:
- Navigate to “API keys” in the sidebar
- Create an API key without expiration
- Test the setup with a CURL request (replace these variables):
- Username
- API key
- dateFrom and dateTo
You should get a response similar to the following:
Great! Now we know that our API key is working and pulling information from our calendar. The API calls we will be using later in this tutorial are:
- /availability: Get your availability
- /bookings: Book a slot
Cerebrium setup
Set up Cerebrium:
- Sign up here
- Follow installation docs here
- Create a starter project:
This creates:
main.py
: Entrypoint filecerebrium.toml
: Build and environment configuration
Add these pip packages to your cerebrium.toml
:
Set up API keys:
-
OpenAI GPT-3.5:
-
Add secrets in Cerebrium dashboard:
- Navigate to “Secrets”
- Add keys:
CAL_API_KEY
: Your Cal.com API keyOPENAI_API_KEY
: Your OpenAI API key
Agent Setup
Create two tool functions in main.py
for calendar management:
- Get availability tool
- Book slot tool
The Cal.com API provides:
- Busy time slots
- Working hours per day
Below is the code to achieve this:
The code above:
- Uses
@tool
decorator to identify functions as LangChain tools - Includes docstrings explaining functionality and required inputs
- Uses
find_available_slots
helper function to format Cal.com API responses into readable time slots
We then follow a similar practice to write our book_slot tool. This will book a slot in my calendar based on the selected time/day. You can get the eventTypeId from your dashboard, select an event and grab the ID in the URL.
Now that we have created our two tools let us create our agent in our main.py
file too:
The above snippet is used to create our agent executor which consists of:
- Our prompt template:
- This is where we can give instructions to our agent on what role it is taking on, its goal and how it should perform in certain situations etc. The more precise and concise this is, the better.
- Chat History is where we will inject all previous messages so that the agent has context on what was said previously.
- Input is new input from the end user.
- We then instantiate our GPT3.5 model that will be the LLM we will be using. You can swap this our with Antrophic or any other provider just by replacing this one line - LangChain makes this seamless.
- Lastly, we join this all together with our tools to create an agent executor.
Setup Chatbot
The above code is static in that it will only reply to our first question but we might need to have a conversation to find a time that suits both the user and my schedule. We therefore need to create a chatbot with tool calling capabilities and the ability to remember past messages. LangChain supports this with RunnableWithMessageHistory().
It essentially allows us to store the previous replies of our conversation in a chat_history variable (mentioned above in our prompt template) and tie this all to a session identifier so your API can remember information pertaining to a specific user/session. Below is our code to implement this:
Let us run a simple local test to make sure everything is working as expected.
The above code does the following:
- We define a Pydantic object which specifies the parameters our API expects - the user prompt and a session id to tie the conversation to.
- The predict function in Cerebrium is the entry point for our API so we just pass the prompt and session id to our agent and print the results.
To run this, simply install the pip dependencies manually by typing the following into your terminal pip install pydantic langchain pytz openai langchain_openai langchain-community
and then run python main.py
to execute your main python file. You will need to replace your secrets with the actual values when running locally. You should then see output similar to the following:
If you keep talking and answering, you will see it will eventually book a slot.
Integrate Langsmith
Production monitoring is crucial, especially for agent applications with indeterministic workflows based on user interactions. LangSmith, a LangChain tool for logging, debugging, and monitoring, helps track performance and handle edge cases. Learn more about LangSmith here.
Set up LangSmith monitoring:
- Add LangSmith to
cerebrium.toml
dependencies - Create a free LangSmith account here
- Generate API key (click gear icon in bottom left)
Next we need to set the following environment variables. You can add the following code at the top of your main.py. You can add the API key to your secrets in Cerebrium
Enable tracing by adding the @traceable
decorator to your functions. LangSmith automatically tracks tool invocations and OpenAI responses through function traversal. Add the decorator to the predict
function and any independently instantiated functions. Edit main.py to have the following:
Easy! Now LangSmith is set up. Run python main.py to run your file and test booking an appointment with yourself.
After you have completed a successful test run you should see data populating in LangSmith. You should see the following:
In the Runs tab, you can see all your runs (ie: invocations/API requests).
In 1 above, it takes the name of our function, input is set to the Cerebrium RunID which in this case we set to “test”. Lastly, you can see the input as well as the total latency of your run.
LangSmith supports various data automations:
- Data annotation for positive/negative case labeling
- Dataset creation for model training
- Online LLM-based evaluation (rudeness, topic analysis)
- Webhook endpoint triggers
- Additional features
You can set these automations by clicking the “Add rule” button above (2) and specifying under what conditions you would like the above to occur. The options to create a rule on are a filter, a sampling rate, and an action.
Lastly, in 3 you can see overall metrics about your project such as number of runs, error rate, latency etc.
LangSmith Threads provide clean conversation tracking between agents and users. Track conversation evolution and investigate anomalies through trace analysis. Each thread links to its associated session ID.
Lastly, you can monitor performance metrics regarding your agent in the Monitor tab. It shows metrics such as trace count, LLM call success rate, First time for token and much more.
LangSmith offers simple integration for agent development with extensive functionality. While we’ve covered the basics, it provides comprehensive support for the application feedback loop: data collection/annotation → monitoring → iteration.
Deploy to Cerebrium
To deploy this application to Cerebrium you can simply run the command: cerebrium deploy in your terminal. Just make sure to delete the name == “main” code since that was just to run locally.
If it deployed successfully, you should see something like this:
You can now call this via an API endpoint and our agent will remember the conversation as long as the session id is the same. Cerebrium will automatically scale up your application based on demand and only pay for the compute you use.
You can find the final version of the code here.
Future Enhancements
Consider implementing:
- Response streaming for seamless user experience
- Email integration for context-aware scheduling when Claire is tagged
- Voice capabilities for phone-based scheduling
Conclusion
The combination of LangChain, LangSmith, and Cerebrium enables scalable agent deployment. LangChain excels at LLM orchestration, tooling, and memory management, while LangSmith provides production monitoring and edge case identification. Cerebrium offers pay-as-you-go scaling across hundreds or thousands of CPU/GPUs.
Tag us as @cerebriumai in extensions you make to the code repository so we can share it with our community.