# Building a Code Assistant

In this guide, we’ll walk through building a powerful **ReAct agent** designed to handle various coding tasks, including code writing, execution, data analysis, and web-based activities. With the help of the **E2B ( code execution ) tool** and a set of specialized integrations, this agent can tackle file management, internet searches, model training, and data analytics. This article will explore each step of the workflow, from creating and configuring the agent to deploying and testing it in real-world scenarios.

For code execution tasks, using a model with advanced programming comprehension is critical. **Claude 3.5 Sonnet** and **GPT-4o** are recommended for their higher-level understanding of programming concepts, which leads to better code generation and efficient problem-solving. These models are well-suited for complex coding environments where accuracy and sophistication are essential.

The code execution feature includes built-in **error handling**. If an exception is raised during code execution, the agent receives the error message and can attempt to correct the code or suggest alternative solutions.

## Getting Started: Building the Workflow

To create a coding agent in React, we'll start by setting up a structured workflow. The essential steps are as follows:

1. **Set up the ReAct agent** — the backbone of your automation.
2. **Integrate E2B as a tool** for code execution, providing a foundation for the agent's coding tasks.

Let's go through these steps in more detail.

***

### Step 1: Creating the React Agent

Before diving into integrations, we'll need to start by building a React-based agent. This setup will serve as the base framework, giving your agent a dedicated environment to operate in.

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2F0y4RhI530yIIzw6vJ2pf%2Fimage.png?alt=media&#x26;token=d220a921-3a1d-47d9-af92-af47705c009c" alt=""><figcaption></figcaption></figure>

The E2B tool is essential for enabling code-writing and execution capabilities in your agent. By integrating E2B, the agent can handle coding tasks directly, making it a valuable asset for development-related workflows.

Defining the agent’s role helps clarify its tasks and purpose. A well-structured prompt can help guide the agent’s actions, especially when it has multiple tools and functionalities.

**Example Prompt:**

> A helpful AI assistant skilled in language, Python programming, and Linux commands. The goal is to provide clear, brief answers to the user. For tasks that require code, first outline a plan, then write well-structured Python code, check for errors, and run it to confirm it works. Use any free, open-source API that doesn’t need authorization, and install necessary packages for handling specific file types, like PDFs or binary files. When working with binary files, understand the file format before reading them.

***

### Step 2: Deploying and Testing the Workflow

After building and configuring your agent, it’s time to deploy and test it across various scenarios to ensure all tools and functionalities work harmoniously.

* Example of using external API call for perform search and do some analysis through generated code

Here is an example of an intermediate step.

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2F8KNu2RhrMeKxnJ2hN1Ku%2Fimage.png?alt=media&#x26;token=632531aa-4cac-4f4f-876c-797e1aee1881" alt=""><figcaption></figcaption></figure>

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2F9QW9E26WClViO7BIaVHY%2Fimage.png?alt=media&#x26;token=8df128d1-2c87-47d4-adfe-43e993487890" alt=""><figcaption></figcaption></figure>

* Example of providing some data generation and modelling issues

<figure><img src="https://4279757243-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTbBxR0Ob7RUmbvHZkQi2%2Fuploads%2FH3RTBITW1x8jgqjruSd0%2Fimage.png?alt=media&#x26;token=6552d8a3-54b8-4ba4-b050-4efe4b113e82" alt=""><figcaption></figcaption></figure>

And final output in proper markdown format.

> #### K-means Clustering Report
>
> **Code**
>
> ```python
> import numpy as np
> import matplotlib.pyplot as plt
> from sklearn.cluster import KMeans
>
> # Generate synthetic data
> np.random.seed(42)  # For reproducibility
> X = np.random.rand(100, 2)  # 100 points in 2D
>
> # Apply K-means clustering
> kmeans = KMeans(n_clusters=3)
> kmeans.fit(X)
> labels = kmeans.labels_
> centers = kmeans.cluster_centers_
>
> # Plot the results
> plt.figure(figsize=(8, 6))
> plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', marker='o', edgecolor='k')
> plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75, marker='X')
> plt.title('K-means Clustering')
> plt.xlabel('Feature 1')
> plt.ylabel('Feature 2')
> plt.grid(True)
> plt.show()
> ```
>
>
>
> **Results**
>
> * **Labels**: The clustering labels for each of the 100 data points are as follows:
>
> ```
> [0, 2, 0, 0, 2, 0, 1, 0, 0, 1, 1, 0, 2, 0, 1, 1, 0, 2, 1, 1, 0, 0, 0, 0, 1, 2, 2, 2, 0, 0, 1, 1, 0, 0, 0, 1, 0, 2, 1, 1, 2, 1, 0, 2, 2, 0, 2, 2, 1, 0, 0, 0, 1, 0, 1, 1, 2, 2, 1, 2, 2, 1, 0, 2, 0, 1, 0, 1, 2, 0, 1, 1, 1, 2, 0, 1, 0, 1, 2, 0, 0, 2, 0, 1, 0, 1, 1, 1, 1, 1, 1, 2, 0, 2, 1, 0, 2, 0, 2, 2]
> ```
>
>
>
> *   **Cluster Centers**: The coordinates of the cluster centers are:
>
>     ```
>     [[0.17284770118135123, 0.59187921496425],
>      [0.593866956168038, 0.19927680984408092],
>      [0.7685495540478937, 0.7122501507434474]]
>     ```
>
>
>
> **Conclusion**
>
> The K-means algorithm successfully clustered the data into 3 groups based on the generated features. The visualization shows the distribution of the data points and the identified cluster centers.
