File: code-consultant.md | Updated: 11/15/2025
Agent Skills are now available! Learn more about extending Claude's capabilities with Agent Skills .
English
Search...
Ctrl K
Search...
Navigation
Prompt Library
Code consultant
Home Developer Guide API Reference Model Context Protocol (MCP) Resources Release Notes
On this page
Copy this prompt into our developer Console to try it for yourself!
| | Content | | --- | --- | | System | Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the codeβs performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency. | | User | def fibonacci(n): <br>if n <= 0: <br>return [] <br>elif n == 1: <br>return [0] <br>elif n == 2: <br>return [0, 1] <br>else: <br>fib = [0, 1] <br>for i in range(2, n): <br>fib.append(fib[i-1] + fib[i-2]) <br>return fib |
Python
Copy
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
Explanation: Using a generator function with yield is more memory-efficient for generating the Fibonacci sequence. It avoids creating and storing the entire sequence in a list. Instead, it generates each number on-the-fly as needed.
Copy
import anthropic
client = anthropic.Anthropic( # defaults to os.environ.get("ANTHROPIC_API_KEY")
api_key="my_api_key",
)
message = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1000,
temperature=0,
system="Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages=[\
{\
"role": "user",\
"content": [\
{\
"type": "text",\
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib",\
}\
],\
}\
],
)
print(message.content)
Was this page helpful?
YesNo
Idiom illuminator Function fabricator
Assistant
Responses are generated using AI and may contain mistakes.