Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Controlling Claude's Behavior

Resource mentioned in this lesson: Trustworthy Agents in Practice

00:00 In the last lesson, you sent your first prompt, but you realize you want more control over Claude’s behavior to use it as a Python-only coding assistant.

00:09 To define Claude’s role, tone, and constraints, you need to use the system parameter. Here’s an example that describes your coding assistant.

00:19 As you can see, it’s a string. It says, """You are a Python coding assistant. You only answer questions about Python. If the user asks about any other programming language or unrelated topics, politely explain that you can only help with Python questions.""" So you explain all the characteristics you want from Claude in descriptive language.

00:39 But before you actually see this in practice, pay attention to the distinction between the role parameter and this system parameter.

00:47 The system prompt is provided once at the beginning, while user and assistant messages alternate throughout the messages array. So this system prompt must only contain the characteristics you want Claude to have throughout your entire conversation. Okay, back to the task.

01:06 Your task is to configure the assistant using system so that it acts as a Python coding assistant, it answers only Python-related questions, and it politely refuses non-Python questions.

01:18 Head over to your code editor and copy-paste the same code from basic_claude_call.py from before. Here I’ve named the new file coding_assistant.py.

01:28 You need to change up a few things here. First things first, after your client, go ahead and copy-paste the same system prompt you just saw that describes your coding assistant.

01:39 Here you go.

01:41 Besides this, you want to capture what the user wants to ask instead of just predefining it. So here, go ahead and create user_input = input(), and then here you can tell the user, "Ask me anything about Python:"

02:02 You also need to make some changes in your response block. You don’t want to change your model or max_tokens, but right after your max_tokens, you need to define your system prompt.

02:13 So you go ahead and say system equals the system prompt that you just copy-pasted.

02:21 Don’t forget the comma.

02:24 And inside of your messages list, go ahead and remove "How do I reverse a string in JavaScript?" Now you’re actually capturing what the user is telling you. Now you need to replace it with what the user will ask you.

02:36 So user_input goes here, and that is all you need to change. You can go ahead and run your code again.

02:46 python coding_assistant.py. Okay, it’s telling you Ask me anything about Python: Okay, now let’s check if it actually refuses to answer unrelated JavaScript questions or not.

02:59 So here’s the same question from before. How do I reverse a string in JavaScript? Let’s see.

03:12 There you go. It says, I can only help with Python questions. Your question is about JavaScript, which is outside my area of assistance. And then it goes ahead and talks about how to reverse a string in Python instead.

03:26 The system prompt actually helped you control Claude’s behavior. However, in real-world or safety-sensitive applications, relying on a system prompt alone is usually not enough. Users might be sneaky and find ways to elicit responses outside the intended scope through techniques like prompt injection or jailbreaks.

03:48 And this is actually an active area of research, and developers often combine several techniques to make applications more immune. Some of these approaches are server-side validation, scoping the model’s capabilities, tool permission controls, and input and output filtering.

04:06 And if you’re specifically interested in building Claude-powered agents or maybe deploying models in production, Anthropic’s Trustworthy Agents blog is an excellent resource for learning more about these techniques.

04:20 Okay, now back to your Python coding assistant. Go ahead and try a Python question this time and pay attention to how the output is structured.

04:30 Run your file again and ask, for example, How do I sort a list? As you can see, the code and explanation are just unstructured text.

04:49 And that’s actually okay if you’re reading it yourself, but it’s much less useful if you want to store the response or search it or maybe even pass it to another part of your application.

05:00 So, how can you make the model return data in a more predictable structured format? That’s where JSON schemas and Pydantic come in. They’re waiting for you in the next lesson.

Become a Member to join the conversation.