Claude API Crash Course #3 - Making the Prompt Dynamic
Chapters7
Switch from a static message to a template string using back ticks to embed variables for ingredients and filters.
Net Ninja shows how to make prompts dynamic by including user-selected items and filters, producing more tailored Claude API recipes.
Summary
In Claude API Crash Course #3, Net Ninja moves beyond a basic prompt and demonstrates how to tailor recipe responses using user inputs. He explains that items (pantry ingredients) and filters (instruction strings) are arrays that influence the AI’s output. By switching to a template string, he inserts dynamic values—joining the items and filters into readable lists—to craft a focused prompt. He even adds a fallback with “no additional constraints” for empty filters. The lesson includes practical tweaks like adding “important” prompts to encourage creativity and a randomized selection of options to avoid repetitive results. Net Ninja tests several configurations, such as limiting to five ingredients and enforcing vegetarian constraints, and checks the results against the expected constraints. The goal is to ensure the model respects user input while keeping the generated recipe readable and varied. The next lesson promises to address readability and formatting for easier consumption. Overall, this video blends code changes with practical prompts to make Claude deliver more relevant recipes based on real user data.
Key Takeaways
- Join the pantry items array into a comma-separated string to feed the AI: items.join(',') which converts an array into a usable list in the prompt.
- Join the filters array into a comma-separated string and include a fallback "no additional constraints" if empty, ensuring the prompt remains well-formed.
- Use a template string (backticks) to embed dynamic values directly into the prompt, enabling real-time customization per user submission.
- Incorporate prompts like "important try to be creative" and "think about five dishes… respond with only one of them at random" to inject variety and avoid repetition.
- Test configurations by toggling constraints (e.g., max five ingredients, keep it veggie) to verify the model adheres to user-selected rules.
Who Is This For?
This is essential viewing for developers integrating Claude API prompts who want user-driven customization (items and filters) to drive more accurate, constrained outputs.
Notable Quotes
"create a recipe from only these ingredients."
—Shows how the prompt begins by anchoring the recipe to the user-provided ingredients.
"apply these constraints to the recipe and then a colon again. And then I'll add another value is going to be filters dojoin again."
—Demonstrates embedding dynamic arrays into the prompt to reflect user-selected filters.
"max five ingredients and keep it veggie."
—Practical example of applying a concrete constraint to validate the prompt works.
"respond with only one of them at random. Do not tell me about the other options."
—Illustrates how to introduce randomness to avoid repeated outputs.
Questions This Video Answers
- how do I pass user pantry items to Claude API in a dynamic prompt?
- how to use template literals to join arrays for Claude prompts
- how to enforce recipe constraints like max ingredients or vegetarian options with Claude API
Claude APIClaude Prompt EngineeringDynamic PromptsJavaScript template literalsArray.join()Post handlerFilters array pantry ingredientsrecipe generation
Full Transcript
So then in the last lesson we managed to send out our first message to the AI model asking it to generate a recipe and we got a response which we then returned down here. However, the recipe that we asked it to generate was completely random and it included none of the pantry ingredients that a user types in and it applied none of the filters they might have selected. Both of which we have access to in this post request handler. So, in this lesson, we're going to make the prompt a little bit more focused and also include those extra details that a user submits so that the recipe we get back is much more tailored to the user's request.
Now, these two things up here, items and filters, they're both arrays. The items is just an array of all the pantry ingredients that a user has added. And the filters are an array of filter instruction strings. Actually, if we open the sidebar folder in the components directory and then open the filter section component, we can see what these filter values are. So, right here, we've got an array called filters uppercase and each filter has a value property, which is an instruction to the AI model eventually. For example, use a maximum of five ingredients or keep the recipe vegetarian and do not use meat ingredients.
So these values things right here, these strings are what's going to be in the filters array inside the post handler depending on which filters the user selects. Anyway, let's go back now and try making the prompt a little bit more uh dynamic by using these different values. So the first thing I'm going to do is delete the entire message that we have so far and I'm going to replace it with back ticks so that we can make a template string and now put variables inside it. Then on the first line, I'm going to say create a recipe from only these ingredients.
Then I'm going to add a column followed by a dollar sign in curly braces to output a value. And that value is going to be the items which I'll use the join method on. And we'll join them with a comma so that we get a commaepparated list of ingredients. And I use that join method by the way because the items value is an array. Okay? And I wanted to convert that into one big string. All right. So on the next line, I'll say apply these constraints to the recipe and then a colon again. And then I'll add another dollar sign and curly braces to output another value which this time is going to be filters dojoin again.
And we'll join those array values with a comma as well. Then I'm going to use a logical or followed by the string no additional constraints. And the reason I'm doing this is because if the filters array is empty, like if we don't select any, I'm going to let the model know there are no constraints rather than just have no value here after this statement. Does that make sense? Okay. So now I'm going to come to the next line and I'm going to paste in two more instructions which say important try to be creative and don't always go for the obvious recipe.
And I put that in because when I was testing this a lot of the time it would come back when I refreshed and pressed the button again with the same recipe. Not always, but a lot of the time. So by putting this in, it kind of mixes it up a little bit. And I've done the same thing down here or a similar thing rather. Um, I've wanted to make it more random by saying important. Before making the recipe, think about five dishes that can be made from the provided ingredients and respond with only one of them at random.
Do not tell me about the other options. So again, this is just to kind of make it so that when you're refreshing or pressing the button again that it doesn't come back with the same recipe all the time. Okay, so then now we're adding the ingredients and the filters to the prompt. Let's try it out and see what happens. Okay then. So, I'm going to apply a couple of filters here. I'm going to say max five ingredients and keep it veggie. That way, we can hopefully easily see if it does the job correctly. Then, I'm going to click on generate recipe.
Just going to wait a moment for it to send that recipe back. And here we have it. So, blue Stilton and leak potato gratin. Yeah, that sounds vegetarian. Let's make sure it's only got five ingredients or less. Um, so potatoes, leaks, blue stilton, and cream. Yeah. Awesome. Oh, and garlic. So, that's five, isn't it? Yeah. 1 2 3 4 5. Then we've got the method that looks as though it's working. Uh let's take off this keep it veggie. We'll keep on this max five ingredients and we'll turn this one on now and generate another recipe.
All right. And this time we've got creamy blue Stilton noodles. Still veggie, but there's nothing saying it has to have meat in it. Uh we still have one, two, three, four, five ingredients as well. Okay, I'm going to turn off this max five ingredients. And I'm going to turn off this filter as well. And I'm going to do one more. All right. And it looks like this time we have got meat in it. It's a similar one. It's creamy blue still and beef and leak noodle bake. And the ingredients are all here. So 1 2 3 4 5 uh six 7 8.
Yeah. So it's no longer applying those filters, but it did apply those filters when we checked them. And it's only using these ingredients as well. All of these things are inside the pantry. Awesome. So that's all working. However, this right here is not very readable. So, we're going to sort that out in the next lesson.
More from Net Ninja
Get daily recaps from
Net Ninja
AI-powered summaries delivered to your inbox. Save hours every week while staying fully informed.







